Saturday, 9 June 2012

Simple programs in java

Armstrong numbers
CODING:
import java.io.*;
class disp
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter the no :");
int a=Integer.parseInt(br.readLine());
System.out.println("Enter the String :");
String s = br.readLine();
System.out.println("Number : " + a + "String :" + s);
}
catch(Exception e)
{
System.out.println("Error.....");
}
}
}
OUTPUT:
Enter the no :
5
Enter the String:
Hi

Odd or even 
CODING:
import java.io.*;
class odd
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.printf("Enter the no :");
int a=Integer.parseInt(br.readLine());
if(a%2==0)
System.out.println( a + " is even");
else
System.out.println( a + " is odd");
}
catch(Exception e)
{
System.out.println( "Error!!");
}
}
}
OUTPUT:
Enter the no:3
3 is odd

 Factorial of a number
CODING:
import java.io.*;
class fact
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("Enter the no :");
int n=Integer.parseInt(br.readLine());
int f=1;
for(int i=n;i>=1;i--)
f=f*i;
System.out.print("Factorial of "+ n + " is " + f );
}
catch(Exception e)
{
System.out.println( "Error!!");
}
}
}
OUTPUT:
Enter the no :5
Factorial of 5 is 120

 Menu driven calculator 
CODING:
import java.io*;
class menu
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          try
          {       
          System.out.printf("Enter a :");         
          int a=Integer.parseInt(br.readLine());
          System.out.printf("Enter b :");
          int b=Integer.parseInt(br.readLine());
System.out.println("1.Addition  2.Subtraction  3.Multiplication        4.Division   5.Modulo   6.Left shift  7.Right shift");
          System.out.printf("Enter your choice :");
          int c=Integer.parseInt(br.readLine());
                   switch(c)
                   {
                   case 1: c=a+b;
                   System.out.println("Addition :" + c);
                   break;
                   case 2: c=a-b;
                   System.out.println("Subtraction :" + c);
                   break;
                   case 3: c=a*b;
                   System.out.println("Multiplication :" + c);
                   break;
                   case 4: c=a/b;
                   System.out.println("Division :" + c);
                   break;
                   case 5: c=a%b;
                   System.out.println("Modulo :" + c);
                   break;
                   case 6: c=a<<2;
                   System.out.println("Left shift :" + c);
                   break;
                   case 7: c=b>>2;
                   System.out.println("Right shift :" + c);
                   break;
                   default:
                   System.out.println("Invalid Data");
                   break;
                   }
          }
          catch(Exception e)
          {
          System.out.println("Error....");
          }
}
}
OUTPUT:
Enter a :4
Enter b :2
1.Addition    2.Subtraction   3.Multiplication   4.Division   5.Modulo 
6.Left shift   7.Right shift
Enter your choice :6
Left shift :16

Armstrong number
CODING:
import java.io.*;
class armstrong
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try
            {
            int n,i,j,m,s=0;
            System.out.printf("Enter a no:");
            n=Integer.parseInt(br.readLine());
            j=n;
                        for(i=0;i<3;i++)
                        {
                        m=n%10;
                        s= s + (m*m*m);
                        n=n/10;
                        }
                        if(j==s)
                        System.out.printf(j + " is an Armstrong number");
                        else
                        System.out.printf(j + " is not an Armstrong number");
            }
            catch(Exception e)
            {
            System.out.println("Error.....");
            }
}
}
OUTPUT:
Enter a no:370
370 is an Armstrong number

Sequence generation 
CODING:
import java.io.*;
class disp
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try
            {
            int k=1,i,j;
            System.out.printf("Enter n:");
            int n=Integer.parseInt(br.readLine());
            System.out.println("\n"+ k);
                        for(i=1;i<n;i++)
                        {
                        System.out.printf("%d",++k);
                        for(j=1;j<=i;j++)
                        {
                        k=k+1;
                        System.out.printf("   %d",k);
                        }
                        System.out.printf("\n");
                        }
            }
            catch(Exception e)
            {
            System.out.println("Error.....");
            }
}
}
OUTPUT:
Enter n:3
1
2   3
4   5   6

 Reverse of a number 
CODING:
import java.io.*;
class rev
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          try
          {
          int r,n,sum=0;
          System.out.printf("Enter a no:");
          n=Integer.parseInt(br.readLine());
                   while(n!=0)
                   {
                   r =n%10;
                   n = n/10;
                   sum=10*sum+r;
                   }
          System.out.printf("Reversed no:" + sum);
          }
          catch(Exception e)
          {
          System.out.println("Error.....");
          }
}
}
OUTPUT:
Enter a no :123
Reversed no:321

 Fibonacci series 
CODING:
import java.io.*;
class fib
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          try
          {
          int n,a=-1,b=1,c,i;
          System.out.printf("Enter a no:");
          n=Integer.parseInt(br.readLine());
          System.out.println("Fibonacci Series:");
                   for(i=0;i<=n;i++)
                   {
                   c=a+b;
                   a=b;
                   b=c;
                   System.out.println(c);
                   }
          }
          catch(Exception e)
          {
          System.out.println("Error.....");
          }
}
}
OUTPUT:
Enter a no :4
0
1
1
2


 Sequence generation 
CODING:
import java.io.*;
class seq
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          try
          {
          int s=-1;
          int k=1,i,temp;
          System.out.println("Sequence:");
          System.out.printf("%d ",k);
                   for(i=2;i<10;i++)
                   {
                   k=k+i;
                   temp=k*s;
                   System.out.printf("   %d  ",temp);
                   s=s*-1;
                   }
          }
          catch(Exception e)
          {
          System.out.println("Error.....");
          }
}
}
OUTPUT:
Sequence: 1    -3    6    -10    15    -21    28    -36    45            
         
 Type conversion 
CODING:
import java.io.*;
class cast
{
public static void main(String args[])
{
          byte b;int i=257;double d=323.123;
          b=(byte)i;
          System.out.println("b =" + b);
          i=(int)d;
          System.out.println("i =" + i);
          b=(byte)d;
          System.out.println("b =" + b);
}
}
OUTPUT:
b=1
i=323
b=67

 Displaying arrays 
 import java.io.*;
class disparr
{
public static void main(String str[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            try
            
            int i;
            System.out.println("Enter size of the array ");
            int n=Integer.parseInt(br.readLine());
            int a[]=new int[n];
            System.out.println("Enter values of Integer array");
            for(i=0;i<n;i++)
            a[i]=Integer.parseInt(br.readLine());
            String s[]=new String[n];
            System.out.println("Enter values of String array");
            for(i=0;i<n;i++)
            s[i]=br.readLine();
            System.out.println("Integer array");
            for(i=0;i<n;i++)
            System.out.println(a[i]);    
            System.out.println("String array");
            for(i=0;i<n;i++)
            System.out.println(s[i]);    
            }
            catch(Exception e)  
            {
            System.out.println("Error....");
            }
}
}

OUTPUT:
Enter size of the array
3
Enter values of Integer array
3
6
5
Enter values of String array
hi
bye
hai
Integer array
3
6
5
String array
hi
bye
hai

 Multiplication of two matrices 
CODING:
import java.io.*;
class matmul
{
public static void main(String str[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            try
            {
           int i,j,k;
            System.out.print(" Enter rows and columns of Matrix A & Matrix B: ");
            int r1=Integer.parseInt(br.readLine());
            int c1=Integer.parseInt(br.readLine());
            int r2=Integer.parseInt(br.readLine());
            int c2=Integer.parseInt(br.readLine());
            int a[][]=new int[r1][c1], b[][]=new int[r2][c2], c[][]=new int[r1][c2];
            System.out.println(" Enter the values of Matrix A ");
            for(i=0;i<r1;i++)
            for(j=0;j<c1;j++)
            a[i][j]=Integer.parseInt(br.readLine());
            System.out.println(" Enter the values of Matrix B:");
            for(i=0;i<r2;i++)
            for(j=0;j<c2;j++)
            b[i][j]=Integer.parseInt(br.readLine());
            if(c1==r2)
            {
            System.out.println(" Multiplication of Two Matrices ");
            for(i=0;i<r1;i++)
            for(j=0;j<c2;j++)
            {
            c[i][j]=0;
            for(k=0;k<c1;k++)
            c[i][j]+=a[i][k]*b[k][j];
            }    
            for(i=0;i<r1;i++)
            {
            for(j=0;j<c2;j++)                 
            System.out.print( c[i][j] + "\t");
            System.out.println();
            }}
            else
            System.out.println(" Multiplication of Two Matrices is Not Possible");
            }
            catch(Exception e)
            {
            }
}
}
OUTPUT:
Enter rows and columns of Matrix A:
2
2
2
2
Enter the values of Matrix A
1
1
1
1
Enter the values of Matrix B:
1
1
1
1
Multiplication of Two Matrices
2       2
2       2


No comments:

Post a Comment