Welcome To Uday Satya Blog

Contact Information


You can Contact me through ---

Yahoo Messanger

Google Talk


Facebook
Orkut
Twitter

How to Instal Tomcat 7 & then Compile & Run a Java Servlet Program




Watch the following video & please do comment with your valuable suggestions & opinions on the Video
Thank u :)


How to Instal Tomcat 7 from Uday Satya on Vimeo.

1-10 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



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
7



09  Program to Demonstrate Command Line Argument

class CommandLine
{
public static void main(String args[])
{
for(int i=0; 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

11-20 PROGRAMS



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 area  of right angled triangle using CLA

class AreaTri
{
public static void main(String s[])
{
int a,b,area;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
area=a*b/2;
System.out.println("Area of Right angled Triangle = "+area);
}
}

OUTPUT :

java AreaTri 5 4

Area of Right angled Triangle = 10


13  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:
10
34
36
37
99


14  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


15  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
{
for(int j = 0; j < a[1].length; j++)
{
System.out.print(" "+ a[i][j]);
} 
System.out.println();
}
}}

OUTPUT :

Number of Row= 2
Number of Column= 3
The elements of the matrix are :
 1 2 3
 4 5 6
16  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         


17  Program to find the difference of two matrices using CLA

class Matrixcla {
public static void main(String s[])  {
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int x,y,k=0;
System.out.println("First Matrix : ");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
x=Integer.parseInt(s[k]);
a[i][j]=x;
k++;
System.out.print(" "+ a[i][j]); } 
System.out.println(); } 
System.out.println("Second Matrix : ");
for(int i = 0; i <2; i++) {
for(int j = 0; j <2; j++) {
y=Integer.parseInt(s[k]);
b[i][j]=y;
k++;
System.out.print(" "+b[i][j]); } 
System.out.println(); }
System.out.println("Difference of two matrix : ");
for(int i = 0; i <2; i++) {
for(int j = 0; j <2; j++)
System.out.print(" "+(a[i][j]-b[i][j]));
System.out.println();
}}}

OUTPUT :
java Matrixcla 6 7 8 9 5 5 5 5
First Matrix :
 6 7
 8 9
Second Matrix :
 5 5
 5 5
Difference of two matrix :
 1 2
 3 4
18  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
19  Program to find the trace of a matrix

class Trace
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int i,j,sum=0;
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();
            }
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        if(i==j)
                           sum=sum+a[i][j];
            }
System.out.println("Trace is : " +sum);
}
}
                                   
OUTPUT :

The given matrix :
1 2 3
4 5 6
7 8 9
Trace is : 15            


20  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