java lab made by deepan
java lab made by deepan
third variable.
Sol:-
import java.util.Scanner;
public class swap_without_third {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please input two numbers");
int a,b;
a=s.nextInt();
b=s.nextInt();
System.out.println("Before Swappping a="+a+" b="+b);
a=a+b;
b=a-b;
a=a-b;
Sol:-
import java.util.Scanner;
public class swap_using_third {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please input two numbers");
int a,b,t;
a=s.nextInt();
b=s.nextInt();
System.out.println("Before Swappping a="+a+" b="+b);
t=a;
a=b;
b=t;
System.out.println("After swapping a="+a+" b="+b);
}
}
Output:-
Program:-2 Write a Program to check entered number is
palindrome or not.
Sol:-
import java.util.Scanner;
public class palindrome {
public static void main(String[] args) {
int n,m,r,sum=0;
System.out.println("Please Input number to check whether is it palindrome
or not");
Scanner s = new Scanner(System.in);
n=s.nextInt();
m=n;
while(n>0){ r=n%10;
sum=sum*10+r;
n=n/10;
}
if(m==sum){ System.out.println("Number is Palindrome");
}
else {
System.out.println("Number is not palindrome");
}
}
}
Output:-
Program:-3 Write a Program to calculate the area of rectangle
using parameterized constructor.
Sol:-
public class rect_area_constructor {
double len,br;
rect_area_constructor(double l, double b){ len =l; br=b;
}
public static void main(String[] args) {
rect_area_constructor r = new
rect_area_constructor(10, 5);
r.area();
}
void area(){
double ar = len*br;
System.out.println("Area = "+ar);
}
}
Output:-
Program:-4 Write a Program to check enter number is Armstrong or
not .
Sol:-
import java.util.Scanner;
public class armstrong {
public static void main(String[] args) {
System.out.println("Input number to check is it armstrong or not");
Scanner s = new Scanner(System.in);
int n=s.nextInt();
int m,r,p;
double sum=0;
p=n;
m=n;
int c=0;
while(n>0){ n=n/10; c++;
}
while(m>0){
r=m%10;
m=m/10;
sum= sum + Math.pow(r,c);
}
if(sum==p){
System.out.println("Number is armstrong");
}
else { System.out.println("Number is not Armstrong");
}
}
}
Output:-
Program:-5 Write a Program to print the pattern
1
23
456
91
Sol:-
public class pattern_eight {
public static void main(String[] args) {
int num=1;
for(int i =1;i<=13;i++){
for(int j = 1 ;j<=i;j++){
System.out.print(num+" ");
num += 1;
}
System.out.println();
}
}
}
Output:-
Program:-6 Write a Program to find the factorial of the given
number .
Sol:-
import java.util.Scanner;
public class factorialfuntion {
public static int findFactorial (int n){
int factorial = 1;
for ( int i = 1 ; i <= n ; ++i){
factorial = factorial*i ;
}
return factorial ;
}
public static void main(String[] args) {
System.out.println(" input number for find factorial");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int factorial = findFactorial(n);
System.out.println("Factorial="+ factorial);
}
}
Output:-
Program 7:- Write a Program to demonstrate the method
overloading.
Sol:-
import java.util.Scanner;
public class overloading_file {
void area(double a , double b){
double ar =a*b;
System.out.println("Area of Rectangle:- "+ar);
}
void area(double r){
double ar2 = 3.14*r*r;
System.out.println("Area of Circle = "+ar2);
}
public static void main(String[] args) {
System.out.println("Input length and breadth to find area of
rectangle");
Scanner s = new Scanner(System.in);
double a=s.nextDouble();
double b=s.nextDouble();
System.out.println("Input radius to find the area of circle");
double r=s.nextDouble();
overloading_file o = new overloading_file();
o.area(a, b);
o.area(r);
}
}
Output:-
Program 8:- Write a Program to define inheritance and show the
method overriding
Sol:-
public class overriding_file {
void show(){
System.out.println("My name is Prabal");
}
public static void main(String[] args) {
overriding_file s = new overriding_file();
s.show();
xyz t = new xyz();
t.show();
}
}
class xyz extends overriding_file{
void show(){
System.out.println("My friend name is ABC");
}
}
Output:-
Program 9:- Write a Program using the static method.
Sol:-
public class static_keyword {
void display(){
System.out.println("Hello , How are you?");
}
static void show(){
System.out.println("I am fine , What about you?");
}
public static void main(String[] args) {
static_keyword s = new static_keyword();
s.display();
static_keyword.show();
}
}
Output:-
Program 10:- Write a Program to demonstrate the use of This
keyword .
Sol:-
public class Student {
String name;
int age;
// Constructor
public Student(String name, int age) {
this.name = name; // 'this.name' refers to the instance
variable
this.age = age; // 'age' is the parameter
}
void display() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}