Saturday, 20 July 2013

Java Progs



JAVA PROGRAMS :

01  Program to Demonstrate simple JAVA program                                                                         

 class A
{
public static void main(String args[])
{
System.out.println("Welcome to my World");
}
}-----------------------------------------------------------------------------------------

Save  the  above  Program  as  A.java

Compile Program  in Command by using javac

C:\>javac A.java
 

Run the Program in Command by using java

C:\>java A

 ---------------------------------------------------------------------------------------------

OUTPUT :
 
Welcome to my World




Program to demonstrate a CLASS                                                                                                       
                         
class Democlass //Defining a class
{
int a=34;//Instance Variable of the class
void showa() //Method of the class
{
System.out.println("a = " +a);
}
}//End of the class

class A
{
public static void main(String args[]) {
Democlass obj = new Democlass();
obj.showa();
}
}

 
OUTPUT :
 
a=34



02  Program to compute the area of a circle                                                                                      

 
class A2
{
public static void main(String args[])
{
int r=10;
System.out.println("Radius of Circle :" + r);
System.out.println("Area of Circle :" + r*r);
}
}


OUTPUT :

Radius of Circle : 10
Area of Circle : 314




03  Program  to demonstrate type casting in JAVA                                                                            

class conversion
{
public static void main(String args[])
{
byte b;
int i=257;
double d=323.142;
System.out.println("\nConversion of int to byte.");
b=(byte) i;
System.out.println("i and b " +i+" "+b);

System.out.println("\nConversion of double to int.");
i=(int)d;
System.out.println("d and i " +d +" "+i);

System.out.println("\nConversion of double to byte.");
b=(byte)d;
System.out.println("d and b " +d + " " +b);
}
}

 OUTPUT :

Conversion of int to byte.
i and b 257 1

Conversion of double to int.
d and i 323.142 323

Conversion of double to byte.
d and b 323.142 67




04  Program to demonstrate the basic Arithmetic Operations                                                     

 class arith
{
public static void main(String args[])
{
int a=10,b=3;

System.out.println("sum is "+(a+b));
System.out.println("difference is "+(a-b));
System.out.println("product is "+(a*b));
System.out.println("quotient is "+(a/b));
System.out.println("remainder is "+(a%b));
}
}


OUTPUT :

sum is 13
difference is 7
product is 30
quotient is 3
remainder is 1
 



05  Program to find largest of three numbers                                                                                  

 class large
{
public static void main(String args[])
{
int a=10,b=30,c=20;

System.out.println("Greatest of given 3 numbers");
if(a>b&&a>c)
System.out.println(+a);

else if(b>c)
System.out.println(+b);

else
System.out.println(+c);

}
}

OUTPUT :

Greatest of given 3 numbers
30


 

06  Program to print first 10 Fibonacci numbers                                                                         

class fib
{
public static void main(String args[])
{
int i=9,f=0,s=1,temp;

System.out.println(+f);

while(i>0)
{
System.out.println(+s);

temp=f+s;
f=s;
s=temp;
i--;
}
}
}

OUTPUT :
0
1
1
2
3
5
8
13
21
34

 
07  Program to Calculate the factorial of a number using RECURSION                                  
 
class Factorial
{
int fact(int n)
{
int result;

if(n==1)
return 1;
result=fact(n-1)*n;

return result;
}
}

 
class Recursion
{
public static void main(String args[]){
Factorial f=new Factorial();
System.out.println("Factorial of 3 is " +f.fact(3));
System.out.println("Factorial of 4 is " +f.fact(4));
System.out.println("Factorial of 5 is " +f.fact(5));
}
}

 
OUTPUT :
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
 




08  Program to print prime numbers using CLA                                                                      

class primecla
{
public static void main(String s[])
{
int n,r;

n=Integer.parseInt(s[0]);

for(int i=2;i<=n;i++)
{
int c=0;
for(int j=1;j<=i;j++)
{
r=i%j;


if (r==0)

c+=1;
}

if(c==2)
System.out.println(i);
}
}
}
OUTPUT :
java primecla 10
2
3
5



09  Program to Demonstrate Command Line Argument                                                       
class CommandLine
{
public static void main(String args[])
{

for(int i=0; i<args.length; i++)
System.out.println("args[" +i + "]: "+args[i]);
}
}
OUTPUT :
 java CommandLine 1 3 4 2 5
args[0]: 1
args[1]: 3
args[2]: 4
args[3]: 2
args[4]: 5

 
10  Program to convert temperature from Fahrenheit to Celsius scale using CLA
 class ftoccla
{
public static void main(String s[])
{
int f;
double c;
f=Integer.parseInt(s[0]);
c=5*(f-32)/9;
System.out.println(f+" fahrenheit = "+c+" celsius");
}
}
 OUTPUT :
java ftoccla 34

34 fahrenheit = 1.0 celsius
 

 11   Program to produce the conversion table for dollar and rupees using CLA

class dollarcla
{
public static void main(String s[])
{
int n,d;
n=Integer.parseInt(s[0]);
System.out.println("Dollars\t\tRupees" );
for(int i=1;i<=n;i++)
{
d=Integer.parseInt(s[i]);
System.out.println(d+"\t=\t"+(d*45) );
}
}
}
 
OUTPUT :
java dollarcla 4 1 34 36 37
Dollars         Rupees
1             =            45
34          =       1530
36          =       1620
37          =       1665




12  Program to print all the elements in an array using CLA                                     
 
class matrixcla
{
public static void main(String s[])
{
int n,m;
n=Integer.parseInt(s[0]);
System.out.println("The length of the matrix is: " +n);
System.out.println("The elements of the matrix are:");
for(int i=1;i<=n;i++)
{
m=Integer.parseInt(s[i]);
System.out.println(m);
}
}
}
 
OUTPUT :
 
java matrixcla 5 10 34 36 37 99
The length of the matrix is: 5
The elements of the matrix are:
5
10
34
36
37
99

 
 
13  Program to find the sum of all the elements in an array                                           
 class Sum
{
public static void main(String args[])
{


double nums[]={1.1,1.2,1.3,1.4,1.5};

double result=0;

System.out.println("The Elements of Array are :");

for(int i=0;i<5;i++)
       {
          System.out.println(nums[i]);
         result=result+nums[i];
      }
System.out.println("Sum is " + result);
}
}
 
OUTPUT :

The Elements of Array are :
1.1
1.2
1.3
1.4
1.5

Sum is 6.5

 
 

 14  Program to print all the elements in a 2D array                                                         
                 
class Matrix
{
public static void main(String s[]) 
{
int a[][]= {{1,2,3},{4,5,6}};
System.out.println("Number of Row= " + a.length);
System.out.println("Number of Column= " + a[1].length);
System.out.println("The elements of the matrix are : ");
for(int i = 0; i <a.length; i++)
{
for(int j = 0; j < a[1].length; j++)
{
System.out.print(" "+ a[i][j]);
} 
System.out.println();
}
}
}


OUTPUT :
\umber of Row= 2
Number of Column= 3

The elements of the matrix are :
 1 2 3
 4 5 6




15  Program to find the sum of two matrices                                                                       

class addm
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,1,1},{0,1,0},{0,0,1}};
int c[][]= new int[3][3];
int i,j;
for(i=0;i<3;i++)
            for( j=0;j<3;j++)
                        c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        System.out.print(+c[i][j]+ "  ");
          
           System.out.println();
            }
}
}

                                   

OUTPUT :

2  3  4
4  6  6
7  8  10     
 
 
 
16  Program to print the UPPER and LOWER triangle of a matrix                                    

 
 

 
class UppLow
{
public static void main(String arsg[])
 {
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
System.out.println("the given matrix:");
 
for(int i=0;i<3;i++)  {
for(int j=0;j<3;j++) 
System.out.print(" "+a[i][j]);
System.out.println(); }
System.out.println("upper triangle of matrix:");
 
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i<=j)
System.out.print(a[i][j] + " ");
else
System.out.print("  "); }
System.out.println(); }
System.out.println("lower triangle of matrix:");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i>=j)
System.out.print(a[i][j] + " ");
else
System.out.print("  "); }
System.out.println();
}
}
}
 
OUTPUT :
 
The given matrix:
1 2 3
4 5 6
7 8 9
 
Upper triangle of matrix:
1 2 3
  5 6
    9
 
Lower triangle of matrix:
1
4 5
7 8 9    
 
 
 
 
 

17  Program to find the transpose of a matrix                                                                          

 

class Transpose

{

public static void main(String args[])

{

int a[][]={{1,2,3},{4,5,6},{7,8,9}};

int i,j;

System.out.println("The given matrix : ");

for(int i = 0; i <3; i++)

            {

            for(int j = 0; j <3; j++)

            System.out.print(a[i][j] + " "); 

            System.out.println();

            }

System.out.println("Transpose of Matrix : ");

for(i=0;i<3;i++)

            {

            for(j=0;j<3;j++)

                        System.out.print(a[j][i]+ "  ");

            System.out.println();

            }

}

}

           

OUTPUT :

 

The given matrix :

1  2  3

4  5  6

7  8  9

Transpose of Matrix :

1  4  7

2  5  8

3  6  9            

                                   





0 comments: