Thursday, December 25, 2008

basic java programs

Setting java path in windows 7:


Computer -> Properties ->  Advanced System Settings -> Environment Variables.


Class Path: C:\Program Files\Java\jre6\lib\rt.jar;






Setting Path: C:\Program Files\Java\jdk1.6.0_27\bin;












1)program to display sum of two numbers:

class sum
{
public static void main(String args[])
{
int a,b,c;
a=10;
b=20;
c=a+b;
System.out.println("sum="+c);
}
}

OUTPUT:

sum=30

2)program to take input through keyboard and perform addition of two numbers:

import java.io.*;
class exonbufferreader
{
public static void main(String rags[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter two numbers");
int a=Integer.parseInt(br.readLine());
int b=Integer.parseInt(br.readLine());
System.out.println("sum= "+(a+b));
}
}

OUTPUT:
enter two numbers
10
20
sum= 30

3)program to print first n numbers:

import java.io.*;
class firstNnumbers
{
public static void main(string args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int a=Integer.parseInt(br.readLine());
for(int i=0;i<=a;i++)
{
System.out.println(+i);
}
}
}
OUTPUT:

enter a number
2
0
1
2
4)program to print biggest of two numbers:

import java.io.*;
class biggestoftwo
{
public static void main(String rags[]) throws IOException
{
BuferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter two numbers");
int a=Integer.parseInt(br.readLine());
int b=Integer.parseInt(br.readLine());
if(a>b)
{
System.out.println("a is bigger");
}
else
{
System.out.println("b is bigger");
}
}
}

OUTPUT:
enter two numbers
20
10
a is bigger

5)program to print factorial of a given number:

import java.io.*;
class factorial
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int n=Integer.parseInt(br.readLine());
int fact=1;
for(int i=n;i>0;i--)
{
fact=fact*i;
}
System.out.println("the factorial of given no is:"+fact);
}
}

OUTPUT:
enter a number
3
the factorial of a given no is: 6


6)program to print whether the given number is palindrome or not:


import java.io.*;
class palindrome
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int n=Integer.parseInt(br.readLine());
int d,sum=0,m;
m=n;
while(n!=0)
{
d=n%10;
sum=sum*10+d;
n=n/10;
}
if(m==sum)
{
System.out.println("Given no is palindrome");
}
else
{
System.out.println("not a palindrome");
}
}
}

7)program to check whether the given number is armstrong or not:

import java.io.*;
class armstrong
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int n=Integer.parseInt(br.readLine());
int d,sum=0,m;
m=n;
while(n!=0)
{
d=n%10;
sum=sum+(d*d*d);
n=n/10;
}
if(m==sum)
{
System.out.println("armstrong");
}
else
{
System.out.println("not a armstrong");
}
}
}

OUTPUT:
enter a number
153
armstrong

8)program to sum of individual digits in a given number:


import java.io.*;
class sumofdigits
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int n=Integer.parseInt(br.readLine());
int d,sum=0;
while(n!=0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
System.out.println("sum of digits is:"+sum);
}
}

OUTPUT:
enter a number
123
sum of digits is 6

9)program to print reverse of given number:


import java.io.*;
class reverse
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int n=Integer.parseInt(br.readLine());
int d,rev=0;
while(n!=0)
{
n=n%10;
rev=rev*10+d;
n=n/10;
}
Sytem.out.println("The reverse of given no is "+rev);
}
}

10)program to print stars of below manner:
*
**
***
****
*****

import java.io.*;
class stars
{
public static void main(String args[]) throws IOException
{
for(int i=0;i<=5;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}

11)program to print sum of n numbers:


import java.io.*;
class sumofnnumbers
{
public static void main(String args[]) throws IOException
{
BufferedReader ar=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int n=Integer.parseInt(ar.readLine());
int sum=0;
for(int i=0;i<=n;i++)
{
sum=sum+i;
}
System.out.println("The sum of numbers is"+sum);
}
}

OUTPUT:
enter a number
5
The sum of numbers is 15

12)program to display multiplication table:

import java.io.*;
class multiplication
{
public static void main(String[] args)
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int n=Integer.parseInt(br.readLine());
for(int i=1;i<=10;i++)
{
System.out.println(n +"*" + i + "=" + (n*i));
}
}
}

OUTPUT:
enter a number
5
displays the multiplication table of 5

13)program to calculate area of circle:


import java.io.*;
class areaofcircle
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter radius ");
double r=Double.parseDouble(br.readLine());
double pi,a;
pi=3.1416;
a=pi*r*r;
System.out.println("area of circle is:" +a);
}
}

OUTPUT:
enter radius
3.00
Area of circle is:28.2744

14) program to display first 'n' numbers:

import java.io.*;
class nnumbers
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number\n");
int n=Integer.parseInt(br.readLine());
System.out.println("The numbers are as follows:");
for(int i=0;i<=n;i++)
{
System.out.println(i);
}
}
}
output:
enter a number
3
The numbers are as follows:
0
1
2
3
15) program to read string and display the string:


import java.io.*;
class string
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a string\n");
String s=br.readLine();
System.out.println("The entered string is as follows:"s);
}
}

output:
enter a string
sanjeev
The entered string is as follows:
sanjeev

16) program to print odd series:

import java.io.*;
class oddseries
{
public static void main(String args[]) throws IOException
{
BufferedReader sv=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number\n");
int n=Integer.parseInt(sv.readLine());
System.out.println("The odd series is as follows\n");
for(int i=0;i<=n;i++)
{
if(i%2==1)
{
System.out.println(i);
}
}
}
}

output:

enter a number
5
the odd series is as follows:
1
3
5

17) program to demonstrate the usage of scanner:


import java.io.*;
import java.util.*;
class scanner
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter two numbers\n");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("sum ="+(a+b));
}
}

output:
enter two numbers
3
4
sum=7

18) program to find length of a given string:
import java.io.*;
class length
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a string\n");
String s=br.readLine();
System.out.println("The length of the string is:"s.length());
}
}

output:
enter a string
sanju
The length of the string is:5

19) program for printing numbers between two given numbers:
import java.io.*;
import java.util.*;
class numbers
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter two numbers\n");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("The numbers are as follows:");
for(int i=a;i<=b;i++)
{
System.out.println(i);
}
}
}

output:
enter two numbers
2
4
The numbers are as follows:
2
3
4

20) program to check whether the given number is prime or not:
import java.io.*;
class primenornot
{
public static void main(String args[]) throws IOException
{
int c=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number\n");
int a=Integer.parseInt(br.readLine());
for(int i=1;i<=a;i++)
{
if(a%i==0)
{
c=c+1;
}
}
if(c==2)
{
System.out.println("primeno");
}
else
{
System.out.println("nt a prime");
}
}
}

output:
enter a number
5
primeno

21) program to print prime series:

import java.io.*;
class primeseries
{
public static void main(String args[]) throws IOException
{
int i,c=0,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number\n");
int a=Integer.parseInt(br.readLine());
System.out.println("The prime series is as follows:");
for(n=1;n<=a;n++)
{
c=0;
for(i=1;i<=a;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
{
System.out.println(n);
}
}
}
}

output:
enter a number
5
The prime series is as follows:
2
3
5

22) program to read values into an array and to display the sum of array elements:

import java.io.*;
class arraysum
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,sum=0;
int[] a=new int[3];
System.out.println("enter elements into an array\n");
for(i=0;i<3;i++)
{
sum="sum+a[i];
}
System.out.println(" sum="+sum);
}
}

23) program for accepting elements into an array and to separate the entered elements into an even array and an odd array:

import java.io.*;
class evenoddarray
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the size of array");
int n=Integer.parseInt(br.readLine());
System.out.println("enter elements into an array\n");
int i,j=0,k=0;
int[] a=new int[n];
int[] e=new int[n];
int[] o=new int[n];
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
e[j]=a[i];
j++;
}
else
{
o[k]=a[i];
k++;
}
}
System.out.println("array elements\n");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}


output:
enter the size of array:
5
enter the elements into an array:
1
2
3
4
5
the array elements are:
1
2
3
4
5
even array elements r:
2
4
odd array elements r:
1
3
5

24) Initialisation of strings to an array:

class arrayinit
{
public static void main(String args[])
{
String arr[]={"one","two","three"};
System.out.println("arr[0] consists of:"+arr[0]);
System.out.println("arr[1] consists of:"+arr[1]);
System.out.println("arr[2] consists of:"+arr[2]);
}
}

OUTPUT:
arr[0] consists of: one
arr[1] consists of: two
arr[2] consists of: three


25) program based on inheritance:

import java.io.*;
class supclass
{
int i,j;
void showij()
{
System.out.println("i & j are"+i +" & " +j);
}
}
class subclass extends supclass
{
int k;
void sum()
{
System.out.println("sum of i,j,k is="+(i+j+k));
}
}
class exoninheritance
{
public static void main(String args[])
{
supclass supobj=new supclass();
subclass subobj=new subclass();
supobj.i=10;
supobj.j=20;
supobj.showij();
subobj.k=30;
subobj.sum();
}
}

OUTPUT:
i & j values are 1o 20
sum of i,j,k is =30

26) program to find factorial of a given number using recursion:

class factorial
{
int fact(int n)
{
int result;
if(n==1)
return 1;
result=n*fact(n-1);
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 6 is"+f.fact(6));
}
}

OUTPUT:
factorial of 3 is 6
factorial of 6 is 720

27)program to demonstrate the usage of try and catch:

class arithmetic
{
public static void main(String args[])
{
int n,d,r;
n=Integer.parseInt(args[0]);
d=Integer.parseInt(args[1]);
try
{
r=n/d;
System.out.println("result is"+r);
}
catch(Exception e)
{
System.out.println("second arg must be non zero");
e.printStackTrace();
}
}
}

28) program to perform addition of two matrices:

import java.io.*;
class matrixadd
{
public static void main(String args[])throws IOException
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the elements into first array \n");
for(i=0;i<3;i++)
{
for(j=0;j
<3;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("enter the elements into second array\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("The elements of first array r as follows\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("The elements of second array r as follows\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println();
}
System.out.println("the addition matrix is as follows\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print((a[i][j]+b[i][j])+"\t");
}
System.out.println();
}
}
}

29) program to find trace of a given matrix:

import java.io.*;
class matrixtrace
{
public static void main(String args[])throws IOException
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int i,j;
int sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter elements into first array:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("the elements of array r as follows:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
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("the trace of the matrix is:"+sum);
}
}

30) program that demonstrate the usage of multiple try and catch:

class multipletry
{
public static void main(String args[])
{
int n,d,r;
try
{
n=Integer.parseInt(args[0]);
d=Integer.parseInt(args[1]);
r=d/n;
System.out.println("result="+r);
}
catch(ArithmeticException e)
{
System.out.println("second arg must be non-zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("please enter two values");
}
catch(NumberFormatException e)
{
System.out.println("the two values must be numbers");
}
}
}

OUTPUT:
compilation: javac multipletry.java
running: java multipletry 10 20
output:result=2
running: java multipletry 10 a
output: the two values must be numbers
running:java multipletry 10 0
output: result=0
running: java multipletry 0 10
output: second number must be non-zero
running: java multipletry 10
output: please enter two values
running: java multiply 10 20 30
output: result=2

31) program that determines the length of a string:

import java.io.*;
import java.util.*;
class stringlength
{
public static void main(String args[])
{
Scanner kb=new Scanner(System.in);
System.out.println("enter a string\n");
String word=new String(kb.nextLine());
System.out.println("the word is"+word);
int len=word.length();
System.out.println("the length of da word is"+len);
}
}

32)program to print particular character in a given string:

import java.io.*;
import java.util.*;
class stringdemo1
{
public static void main(String args[]) throws IOException
{
String strobj1="first string";
String strobj2="second string";
String strobj3=strobj1+"AND"+strobj2;
System.out.println("the 3rd character in strobj1 is: "+strobj1.charAt(2));
System.out.println("the 4th character in strobj2 is: "+strobj2.charAt(3));
System.out.println("the 1st character in strobj1 is: "+strobj1.charAt(0));
}
}

33) program to perform increment and decrement operations:

import java.io.*;
class increment
{
public static void main(String args[])
{
int x=5;
System.out.println(x);
System.out.println(--x);
System.out.println(x);
System.out.println(++x);
System.out.println(x);
System.out.println(x-- + x--);
System.out.println(x);
System.out.println(x++ + x++);
System.out.println(x);
System.out.println(++x + ++x);
System.out.println(x);
System.out.println(--x + --x);
System.out.println(x);
}
}

OUTPUT:
5
4
4
5
5
9
3
7
5
13
7
11
5

34) program that demonstrates the usage of conditional operator(ternary operator):

class logicalop
{
public static void main(String args[])
{
int a=10;
int b=20;
int c=(a>b)?a:b;
int d=(aSystem.out.println(c);
System.out.println(d);

/*If expression1 being evaluated is true,experssion2 will be performed.If expression1 is false expression3 will b performed.*/
}
}

OUTPUT:
20
10

35) program that demonstrate the usage of bitwise operators:

class bitwiseop
{
public static void main(String args[])
{
int a,b,c,d,e,f;
a=10;
b=15;
c=a&b;
d=ab;
e=a^b;
f=~a;
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
}
}

OUTPUT:
10
15
5
-11

36) program that demonstrate the usage of bitwise shift operators:

class bitwiseshiftop
{
public static void main(String args[])
{
int x=10;
int y=20;
int a,b,c;
a=x<<1; b="x">>1;
System.out.println(a);
System.out.println(b);
}
}

OUTPUT:

20
5

37) program to perform multiple arithmetic operations using switch case:

import java.io.*;
import java.util.*;
class exonswitch
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b,choice;
System.out.println("menu:");
System.out.println("1.addition");
System.out.println("2.subtraction");
System.out.println("3.multiplication");
System.out.println("4.division");
System.out.println("5.modulo");
System.out.println("enter values for a and b:");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
System.out.println("enter ur choice:");
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
System.out.println("addition is "+(a+b));
break;
case 2:
System.out.println("subtraction is "+(a-b));
break;
case 3:
System.out.println("multiplication is "+(a*b));
break;
case 4:
System.out.println("division is "+(a/b));
break;
case 5:
System.out.println("modulo is "+(a%b));
break;
default:System.out.println("wrong choice");
}
}
}

OUTPUT:
menu
1.addition
2.subtraction
3.multiplication
4.division
5.modulo
enter values for a and b:
10 20
enter ur choice:
1
addition is 30

38) program to print palindrome series:


import java.io.*;
class palindrome1
{
public static void main(String rags[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
palinfunction(i);
}
}
public static void palinfunction(int n)
{
int d,sum=0,m;
m=n;
while(n!=0)
{
d=n%10;
sum=sum*10+d;
n=n/10;
}
if(m==sum)
{
System.out.println(sum);
}
}
}