A- 1. Program to assign two integer values to X and Y.
Using the ‘if’ statement the output of the program should display a
message whether X is greater than Y.
class main
{
public static void main (String args[])
{
int x=20,y=20; if(x==y)
System.out.println("Both are equal"); else if(x>y)
System.out.println(x+ "is greater "); else
System.out.println(y+ " is greater ");
}
}
2. Program to list the factorial of the numbers 1 to 10.
class factorial
{
public static void main (String args[])
{
int count=1; long
factorial=1;
System.out.println("Number " + " Factorial"); while(count<=10)
{
factorial=factorial*count;
System.out.println(" "+count+" "+factorial); count++;
}
}
}
3.Program to add two integers and two float numbers.
public class overloadmethod
{
public int functionname(int x,int y)
{
return (x+y);
}
public int functionname(int x,int y,int z)
{
return (x+y+z);
}
public double functionname(double x,double y)
{
return (x+y);
}
public static void main(String args[])
{
overloadmethod s = new overloadmethod();
System.out.println(s.functionname(10,20));
System.out.println(s.functionname(10,20,30));
System.out.println(s.functionname(10.5,19.5));
}
}
5. Program with class variable that is available for all instances of a class.
class mathoperator
{
static double mul(double x,double y)
{
return x*y;
}
static double div(double x,double y)
{
return x/y;
}
}
class mathapplication
{
public static void main(String args[]) { double a =
mathoperator.mul(4.0,5.0); double b =
mathoperator.div(a,2.0);
System.out.println("b = "+b);
System.out.println("a = "+a); }
}
4.Program to perform mathematical operations.
class addsub
{
int num1; int num2;
addsub(int n1,int n2)
{
num1=n1; num2=n2;
}
int add()
{
return num1+num2;
}
int sub()
{
return num1-num2;
}
public void display()
{
System.out.println("Number 1 is "+num1);
System.out.println("Number 2 is "+num2);
}
}
class multidiv extends addsub
{
public
multidiv(int n1,int n2)
{
super(n1,n2);
}
int mul()
{
return num1*num2;
}
float div()
{ return num1/num2;
}
public void display()
{
System.out.println("Number 1 is "+num1);
System.out.println("Number 2 is "+num2);
}
} public class
adsb
{ public static void main(String args[])
{ addsub r1 = new addsub(50,20);
int ad = r1.add(); int sb =
r1.sub(); r1.display();
System.out.println("Addition= "+ad);
System.out.println("Subtraction=
"+sb); multidiv r2 = new multidiv(20,4);
int ml = r2.mul(); float dv = r2.div();
r2.display();
System.out.println("Multiplication= "+ml);
System.out.println("Division= "+dv); } }