Anda di halaman 1dari 16

1|Page

.
PROGRAM IN JAVA TO FIND GREATEST OF TWO NUMBERS
.
class grsm
{
public static void main(String args[])
{
int a=2,b=5;
if(a>b)
{

System.out.println(a+" is greater");
System.out.println(b+" is greater");
}
else
{
System.out.println(b+ " is greater");
System.out.println(a+" is smaller");
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

2|Page
.
PROGRAM IN JAVA TO CONVERT TEMPERATURE IN FARENHIEGHT TO CELSUIS
.
class conv
{
public static void main(String args[])
{
int f=50,c;
c=5*(f-32)/9;
System.out.println("temperature in celcius is "+c);
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

3|Page

PROGRAM IN JAVA TO FIND DISCRIMINENT


.
class disc
{
public static void main(String args[])
{
int a=2,b=5,c=3,d;
d=b*b-4*a*c;
System.out.println("Discriminant is "+d);
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

4|Page

PROGRAM IN JAVA TO FIND SUM OF TWO NUMBERS


.
class sum
{
public static void main(String args[])
{
int a=50,b=60,c;
c=a+b;
System.out.println("sum of a & b is "+c);
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

5|Page

PROGRAM IN JAVA TO SHOW USE OF METHOD OVERLOADING

class A
{
void sum()
{
int a=5,b=7,c;
c=a+b;
System.out.println(c);
}
void sum(int a,int b)
{
int c;
c=a+b;
System.out.println(c);
}

}
class B
{
int sum()
{
int a,b,c;
a=10;

MEHMOOD-UL-NAZAR
18-ITE-09

6|Page
b=40;
c=a+b;
return(c);
}
int sum(int a,int b)
{
int c;
c=a+b;
return(c);
}
}
class Sum1
{
public static void main(String args[])
{
int s,d;
A x=new A();
B y=new B();
s=y.sum();
System.out.println(s);
d=y.sum(50,60);
System.out.println(d);
x.sum();
x.sum(10,30);
}}

MEHMOOD-UL-NAZAR
18-ITE-09

7|Page

PROGRAM IN JAVA TO SWAP TWO NUMBERS WITHOUT USING THIRD VARIABLE


.
class swap
{
public static void main(String args[])
{
int a=2,b=5;
a=a+b;
b=a-b;
a=a-b;
{
System.out.println("Swapped numbers are "+a+"and" +b);
}
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

8|Page

PROGRAM IN JAVA TO FIND THE TYPE OF TRIANGLE


.
class triangle
{
public static void main(String args[])
{
int a=1,b=5,c=2;
if(a==b && b==c)
{
System.out.println("triangle is equilateral");
}
else if(a==b || b==c || c==a)
{
System.out.println("triangle is isosceles");
}
else
{
System.out.println("triangle is secant");
}
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

9|Page

.
PROGRAM IN JAVA TO SHOW USE OF SWITCH STATEMENT
.
class week
{
public static void main(String args[])
{
int i=8;
switch(i)
{
case 1:
{
System.out.println("the day is monday");
}
break;
case 2:
{
System.out.println("the day is tuesday");
}
break;
case 3:
{
System.out.println("the day is wednesday");
}
break;
MEHMOOD-UL-NAZAR
18-ITE-09

10 | P a g e
case 4:
{
System.out.println("the day is thursday");
}
break;
case 5:
{
System.out.println("the day is friday");
}
break;
case 6:
{
System.out.println("the day is saturday");
}
break;
case 7:
{
System.out.println("the day is sunday");
}
break;
default:
{
System.out.println("invalid option");
}
}}}

MEHMOOD-UL-NAZAR
18-ITE-09

11 | P a g e
.
PROGRAM IN JAVA TO SHOW EXCEPTION HANDLING
.
class Exc
{
public static void main(String args[])
{
try
{
int a=args.length;
int b=55/a;
System.out.println("a="+a);
try
{
if(a==1)
{
a=a/(a-a);
}
if(a==2)
{
int c[]={1};
c[55]=99;
}
}
catch(Exception e)
{
MEHMOOD-UL-NAZAR
18-ITE-09

12 | P a g e
System.out.println(e);
}
}
catch(ArithmeticException e)
{
System.out.println(e);
}

}
}

MEHMOOD-UL-NAZAR
18-ITE-09

13 | P a g e
.
PROGRAM IN JAVA TO IMPLEMENT INTERFACES

interface ab
{
void show()
}
class abc implements ab
{
void show();
{
System.out.println("in inter ab");
}
void print()
{
System.out.println( "in abc");
clas demo
{
public static void main(String args[])
{
abc b=new abc();
b.show();
b.print();
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

14 | P a g e

PROGRAM IN JAVA TO IMPLEMENT INHERTENCE

class abc
{
void show()
{
System.out.println("i m in base class show()");
}
}
class def extends abc
{
void show()
{
System.out.println("i m in derived class show()");
}
}
class ghi
{
public static void main(String args[])
{
abc a=new abc();
a.show();
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

15 | P a g e
.
PROGRAM IN JAVA TO PRINT * IN SERIES

class pattern
{
public static void main(String arg[])
{
int i,j;
{
for(i=0;i<5;i++)
{
for(j=0;j<=5;j++)
System.out.println("*");
}
}
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

16 | P a g e

PROGRAM IN JAVA TO FIND FACTORIAL OF A NUMBER


.
class Factorial
{
public static void main(String args[])
{
int n=5,f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
System.out.println(f);
}
}

MEHMOOD-UL-NAZAR
18-ITE-09

Anda mungkin juga menyukai