Java Program Index Page

  •  BASIC PROGRAMS :

           1. To Print Hello World

    • ADDITION PROGRAMS USING VARIOUS METHOD :

          1. To add two number where numbers are already given or initialized
          2. To add two numbers where numbers will be given by user. Using Scanner method
          3. To add two numbers given by user using DataInputStream method
          4. To add two numbers where numbers will be given by user. Using java swing

    • SWAPPING PROGRAMS USING VARIOUS INPUT METHODS :

    1.  To find area of rectangle .  
    2.  Basic Calculator .   

    • IF - ELSE PROGRAMS :

    1.  To find the largest of three numbers
    2. Program to check whether a year entered is a leap year or not.
    3. To Check a number is Armstrong or not up to 3 digit
    4. Digit Checker program which checks the digit of the number up to 5 digit.
    5. To convert numbers between 100 and 999 into words form.

    • FOR LOOP PATTERNS :
    1. To print a Floyd's triangle
    2. To print a star pattern in odd numbers in form of right triangle
    3. To print a star pattern in form of right angle triangle
    4. To print a pattern or star triangle pattern

    • CONVERSION PROGRAM :
    1. To convert decimal to binary from 1 to 1024 numbers using if-else
    2. To convert binary to decimal from 1 to 2048 using if - else only.
    • MORE PROGRAMS :
    1. To solve linear equation
    2. Program which gives area,volume and surface areas of various geometric figures

    Program which gives area,volume and surface areas of various geometric figures

    PROGRAM :

    import java.util.Scanner;
    public class AdvancedCalculator {
       
        public static void main(String[] args) {
          
            int i,j;
            double l,w,z, r;
            System.out.println("THIS IS ADVANCED CALCULATOR OF MATHS");
            System.out.println("1. AREA");
            System.out.println("2. VOLUMES");
            System.out.println("3. SURFACE AREAS");
            System.out.println("ENTER YOUR CHOICE FOR WHAT YOU WANT TO CALCULATE");
            Scanner s = new Scanner (System.in);
            i=s.nextInt();
            switch (i)
            {
            case 1 :
            System.out.println("YOU HAVE CHOSEN AREA");
            System.out.println("YOU HAVE FOLLOWING FORMULAS:");
            System.out.println("1. Area of rectangle");
            System.out.println("2. Area of square");
            System.out.println("3. Area of triangle");
            System.out.println("4. Area of circle");
            System.out.println("5. Area of trapezoid");
            System.out.println("6. Area of ellipse");
            System.out.println("Enter your choice:");
            j=s.nextInt();
            switch(j)
            {
            case 1:
            System.out.println("enter the length  rectangle");
           
            l=s.nextDouble();
            System.out.println("enter the width of rectangle");
            w=s.nextDouble();
            r= l*w;
            System.out.println(" Area of rectangle is :"+r);
            break;
           
            case 2:
            System.out.println("enter the sides of square");
           
            l=s.nextDouble();
            r=l*l;
            System.out.println("Area of square is:"+r);
            break;
           
            case 3:
            System.out.println("Enter the base of triangle");
           
            l=s.nextDouble();
            System.out.println("enter the height of triangle");
            w=s.nextDouble();
            r=0.5*l*w;
            System.out.println(" Area of triangle is"+r);
            break;
           
            case 4:
            System.out.println("enter the radius of circle");
           
            l=s.nextDouble();
            r=3.141*l*l;
            System.out.println(" Area of circle is"+r);
            break;
           
            case 5:
            System.out.println("enter the base1 of trapezoid");
           
            l=s.nextDouble();
            System.out.println("enter the base2 of trapezoid");
            w=s.nextDouble();
            System.out.println("enter the height of trapezoid");
            z=s.nextDouble();
            r=0.5*(l+w)+z;
            System.out.println(" Area of trapezoid is"+r);
            break;
           
            case 6:
            System.out.println("enter the radius of mazor axis");
           
            l=s.nextDouble();
            System.out.println("enter the radius of minor axis");
            w=s.nextDouble();
            r=3.141*l*w;
            System.out.println("Area of ellipse is:"+r);
            break;
           
            default :
            System.out.println("Wrong choice");
            break;
           
            }
            break;
           
            case 2:
            System.out.println("YOU HAVE CHOSEN VOLUME");
            System.out.println("YOU HAVE FOLLOWING FORMULAS:");
            System.out.println("1. Volume of cuboid");
            System.out.println("2. volume of cube");
            System.out.println("3. volume of cylinder");
            System.out.println("4. volume of prism");
            System.out.println("5. volume of sphere");
            System.out.println("6. volume of pyramid");
            System.out.println("7. Volume of right circular cone");
               System.out.println("Enter your choice:");
            j=s.nextInt();
            switch(j)
            {
            case 1:
            System.out.println("enter the length , breadth and height of cuboid");
           
            l=s.nextDouble();
            w=s.nextDouble();
            z=s.nextDouble();
            r=l*w*z;
            System.out.println(" Volume of cuboid is:  "+r);
            break;
           
            case 2:
            System.out.println("enter the side of cube ");
           
            l=s.nextDouble();
            r=l*l*l;
            System.out.println(" Volume of cube is :"+r);
            break;
           
            case 3:
            System.out.println("Enter the radius of cylinder");
           
            l=s.nextDouble();
            System.out.println("Enter the height of cylinder");
            w=s.nextDouble();
            r=3.141*l*l*w;
            System.out.println("volume of cylinder is :  "+r);
            break;
           
            case 4:
            System.out.println("Enter the are of base of prism");
           
            l=s.nextDouble();
            System.out.println("Enter the height");
            w=s.nextDouble();
            r=l*w;
            System.out.println("volume of prism is: "+r);
            break;
           
            case 5:
            System.out.println("Enter the radius of sphere");
           
            l=s.nextDouble();
            r=1.33333*3.141*l*l*l;
            System.out.println("volume of sphere is "+r);
            break;
           
            case 6:
            System.out.println("Enter the base value of pyramid");
           
            l=s.nextDouble();
            System.out.println("Enter the height of pyramid  ");
            w=s.nextDouble();
            r=0.333*l*w;
            System.out.println("volume of pyramid is  "+r);
            break;
           
            case 7:
            System.out.println("Enter the radius of circular trip of cone");
           
            l=s.nextDouble();
            System.out.println("Enter the height");
            w=s.nextDouble();
            r=0.333*3.141*l*l*w;
            System.out.println("volume of right circular cone is  "+r);
            break;
           
            default :
            System.out.println("wrong choice!!!! ");
            break;
           
           
           
            }
            break;
           
            case 3 :
            System.out.println("YOU HAVE CHOSEN SURFACE AREAS");
            System.out.println("YOU HAVE FOLLOWING FORMULAS:");
            System.out.println("1. Surface area of cube");
            System.out.println("2. surface area of rectangular prism");
            System.out.println("3. surface area of sphere");
            System.out.println("4. surface area of cylinder");
               System.out.println("Enter your choice:");
            j=s.nextInt();
            switch(j)
            {
            case 1:
            System.out.println("enter the side of cube ");
           
            l=s.nextDouble();
            r=6*l*l;
            System.out.println("Surface area of cube is   "+r);
            break;
           
            case 2:
            System.out.println("enter the three sides of rectangular prism");
           
            l=s.nextDouble();
            w=s.nextDouble();
            z=s.nextDouble();
            r=2*l*w+2*w*z+2*z*l;
            System.out.println(" Surface area of rectangular prism is   :    "+r);
            break;
           
            case 3:
            System.out.println("enter the radius");
           
            l=s.nextDouble();
            r=4*3.141*l*l;
            System.out.println(" Surface area of sphere is   "+r);
            break;
           
            case 4:
            System.out.println("enter the height and radius");
           
            l=s.nextDouble();
            w=s.nextDouble();
            r= 2*3.141*w*w+2*3.141*l;
            System.out.println(" Surface area of cylinder is  "+r);
            break;
           
            default :
            System.out.println("wrong choice !!!!");
            break;
            }
            break;
             
            default :
            System.out.println("wrong choice!!!!");
            break;
           
            }        
        }
    }

    OUTPUT :

    THIS IS ADVANCED CALCULATOR OF MATHS
    1. AREA
    2. VOLUMES
    3. SURFACE AREAS
    ENTER YOUR CHOICE FOR WHAT YOU WANT TO CALCULATE
    1
    YOU HAVE CHOSEN AREA
    YOU HAVE FOLLOWING FORMULAS:
    1. Area of rectangle
    2. Area of square
    3. Area of triangle
    4. Area of circle
    5. Area of trapezoid
    6. Area of ellipse
    Enter your choice:
    1
    enter the length  rectangle
    6
    enter the width of rectangle
    5
     Area of rectangle is :30.0

    To solve linear equation

    PROGRAM :

    import java.util.Scanner;
    public class LinearEquation {
     
        public static void main(String[] args) {
          
            System.out.println("The linear equation is in the form: aX +bY+c = 0");
            double a,b,c,a1,b1,c1,x,y;
            System.out.println("Enter the value of first linear equation a:");
            Scanner s= new Scanner(System.in);
            a=s.nextDouble();
             System.out.println("Enter the value of first linear equation b:");
            b=s.nextDouble();
             System.out.println("Enter the value of first linear equation c:");
            c=s.nextDouble();
             System.out.println("Enter the value of second linear equation a:");
             a1=s.nextDouble();
             System.out.println("Enter the value of first linear equation b:");
             b1=s.nextDouble();
             System.out.println("Enter the value of second linear equation c:");
             c1=s.nextDouble();
             if(a/a1 != b/b1)
             {
              x= (b*c1 - b1*c)/(a*b1 - a1*b);
              y= (c*a1 - c1*a)/ (a*b1 - a1*b);
              System.out.println("UNIQUE SOLUTION and INTERSECT ALSO");
              System.out.println("values of x : "+x );
              System.out.println("values of y : "+y );
             
             }
             else if (a/a1==b/b1 && b/b1==c/c1)
             {
              System.out.println("infinitely many solutions");
             }
             else if (a/a1==b/b1 && b/b1!=c/c1)
             {
              System.out.println("no solutions");
             }
             
      }
    }

    OUTPUT :

    The linear equation is in the form: aX +bY+c = 0
    Enter the value of first linear equation a:
    9
    Enter the value of first linear equation b:
    6
    Enter the value of first linear equation c:
    3
    Enter the value of second linear equation a:
    5
    Enter the value of first linear equation b:
    4
    Enter the value of second linear equation c:
    9
    UNIQUE SOLUTION and INTERSECT ALSO
    values of x : 7.0
    values of y : -11.0

    To convert binary to decimal from 1 to 2048 using if - else only.

    PROGRAM :

    import java.util.Scanner;
    public class BinaryDecimal {
        public static void main(String[] args) {
         
         System.out.println("This is program for binary to decimal");
         int a,b,c,d=0,e,f=0,h=0,g=0,k,kk=0,i=0,j=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,t=0,u=0,v=0,w=0;
            int x=0,y=0;
            System.out.println("Enter the Binary number");
            Scanner s =new Scanner (System.in);
            a= s.nextInt();
            b= a%10;
            c= a/10;
            if (c>0)
            {
               d= c%10;
               e=c/10;
                if(e>0)
               {       f= e%10;
                        g=e/10;
                }
                 if(g>0)
                 {
                    h= g%10;
                   i=g/10;
                 }
                 if (i>0)
                   {
                  j=i%10;
                  kk=i/10;
                    }
                  if(kk>0)
                   {
                  l=kk%10;
                m=kk/10;
                    }
                  if(m>0)
                     {
                  n=m%10;
                o=m/10;
                    }
                  if(o>0)
                   {
                     p=o%10;
                  q=o/10;
                  }
                  if(q>0)
                   {
                  r=q%10;
                  t=q/10;
                  }
                   if(t>0)
                   {
                  u=t%10;
                  v=t/10;
                    }
                  if(v>0)
                   {
                  w=v%10;
                x=v/10;
                    }
                  if(x>0)
                   {
                  y=x%10;
                    }
                }
                             
            b=b*1;
            d= d*2;
            f= f*4;
            h= h*8;
            j= j*16;
            l=l*32;
            n=n*64;
            p=p*128;
            r=r*256;
            u=u*512;
            w=w*1024;
            y=y*2048;
            k=b+d+f+h+j+l+n+p+r+u+w+y;
            System.out.println("the decimal is :" +k);
              }
    }

    OUTPUT :

    This is program for binary to decimal
    Enter the Binary number
    101
    the decimal is :5

    Java Program Index Page

    •  BASIC PROGRAMS :

             1. To Print Hello World

      • ADDITION PROGRAMS USING VARIOUS METHOD :

            1. To add two number where numbers are already given or initialized
            2. To add two numbers where numbers will be given by user. Using Scanner method
            3. To add two numbers given by user using DataInputStream method
            4. To add two numbers where numbers will be given by user. Using java swing

      • SWAPPING PROGRAMS USING VARIOUS INPUT METHODS :

      1.  To find area of rectangle .  
      2.  Basic Calculator .   

      • IF - ELSE PROGRAMS :

      1.  To find the largest of three numbers
      2. Program to check whether a year entered is a leap year or not.
      3. To Check a number is Armstrong or not up to 3 digit
      4. Digit Checker program which checks the digit of the number up to 5 digit.
      5. To convert numbers between 100 and 999 into words form.

      • FOR LOOP PATTERNS :
      1. To print a Floyd's triangle
      2. To print a star pattern in odd numbers in form of right triangle
      3. To print a star pattern in form of right angle triangle
      4. To print a pattern or star triangle pattern
      • CONVERSION PROGRAM :
      1. To convert decimal to binary from 1 to 1024 numbers using if-else

      To print a Floyd's triangle

      PROGRAM :

      public class Floyd {
                public static void main(String[] args) {
             
              int i,j,n=1;
              for(i=1;i<10;i++)
              {
              for(j=1;j<i;j++){
             
              
              System.out.print(+j);
              }
              System.out.println();
             
          }
      }
      }

      OUTPUT :

      1
      12
      123
      1234
      12345
      123456
      1234567
      12345678

      To convert decimal to binary from 1 to 1024 numbers using if-else

      PROGRAM :

      public class BinaryC {
              
        
          public static void main(String[] args) {
            
              
               System.out.println("this is program for conversion of decimal to binary");
              System.out.println("enter the number between 1 and 1024");
              Scanner s = new Scanner (System.in);
              int a,b,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,t=0,u=0,v=0,w=0;
              int x=0;
              a=s.nextInt();
              if (a%2==0)
              { b=0;
                c=a/2;}
                else  {b=1;c=a/2;
                }
              
              if( c > 0 && c%2==0)
              { d=0;
                e=c/2;}
                else if(c>0) { d=1;e=c/2;
                }
              
               if( e > 0 && e%2==0)
              { f=0;
                g=e/2;}
                else if(e>0) { f=1;g=e/2;
                }
              
               if( g > 0 && g%2==0)
              { h=0;
                i=g/2;}
                else if(g>0) { h=1;i=g/2;
                }
               if( i > 0 && i%2==0)
              { j=0;
                k=i/2;}
                else if(i>0) { j=1;k=i/2;
                }
               if( k > 0 && k%2==0)
              { l=0;
                m=k/2;}
                else if(k>0) { l=1;m=k/2;
                }
                 if( m > 0 && m%2==0)
              { n=0;
                o=m/2;}
                else if(m>0) { n=1;o=m/2;
                }
                 if( o > 0 && o%2==0)
              { p=0;
                q=o/2;}
                else if(o>0) { p=1;q=o/2;
                }
                 if( q > 0 && q%2==0)
              { r=0;
                t=q/2;}
                else if(q>0) { r=1;t=q/2;
                }
                 if( t > 0 && t%2==0)
              { u=0;
                v=t/2;}
                else if(t>0) { u=1;v=t/2;
                }
                 if( v > 0 && v%2==0)
              { w=0;
                x=v/2;}
                else if(v>0) { w=1;x=v/2;
                }
                
              
              System.out.println("the binary is :"+w +u +r +p +n +l +j +h +f +d +b);
      }
      }
              

      OUTPUT :

      this is program for conversion of decimal to binary
      enter the number between 1 and 1024
      5
      the binary is :00000000101

              
              

      To print a star pattern in odd numbers in form of right triangle

      PROGRAM :

      public class Star {         

       public static void main(String[] args) {             

         int i,j;

              for (i=1; i<=5; i++)

              {

               for (j=1; j<=(2*i-1); j++)

               {

               System.out.print(" *");

               }

               System.out.println();

       }

       }

      }

      OUTPUT :

       *
       * * *
       * * * * *
       * * * * * * *
       * * * * * * * * *

      To print a star pattern in form of right angle triangle

      PROGRAM :

      public class Star {         

       public static void main(String[] args) {             

         int i,j;

              for (i=1;i<=5;i++)

              {

              for (j=1;j<=i;j++)

              {

              System.out.print(" *");

              }

              System.out.println();

       }

       }

      }



      OUTPUT :

       *
       * *
       * * *
       * * * *
       * * * * *

      To print a pattern or star triangle pattern

      PROGRAM :

      public class Pattern {         

       public static void main(String[] args) {             

        int i,j,k=0;       

       for (i=1;i<=5;i++)      

        {       

      for (j=1;j<=5-i;j++)        

      {       

      System.out.print(" ");       

      }       

      while(k != (2*i-1)){                       

      System.out.print("*");      

        k++;        

      }      

        k=0;       

       System.out.println();       

       }          

        }

      }



      OUTPUT :

           *
         ***
        *****
       *******
      *********

      To find the largest of three numbers

      PROGRAM :

      import java.util.Scanner;
      public class Largest {
               public static void main(String[] args) {
           
              System.out.println("enter the first number");
              Scanner s = new Scanner(System.in);
              int a,b,c;
              a= s.nextInt();
              System.out.println("enter the second number");
              b= s,nextInt();
              System.out.println("enter the third number");
              c= s.nextInt();
              if (a> b)
                {
                        if (b>c)
                               {   System.out.println("largest number is :"+a);
                                }
                }
                 if (b> c)
                {
                        if (c>a)
                               {   System.out.println("largest number is :"+b);
                                }
                }
                if (c> b)
                {
                        if (b>a)
                               {   System.out.println("largest number is :"+c);
                                }
                }
            else
                System.out.println("all numbers are   equal);
      }
      }

      Program to check whether a year entered is a leap year or not.

      PROGRAM :

      import java.util.Scanner;
      public class Leap {
               public static void main(String[] args) {
           
              System.out.println("enter the year to check whether it is leap year or not");
              Scanner s = new Scanner(System.in);
              int a;
              a= s.nextInt();
              if ( a % 4== 0 )
              System.out.println("the year entered is leap year");
              else
             System.out.println("the year is not a leap year");
      }
      }

      To Check a number is Armstrong or not up to 3 digit

      PROGRAM :

      import java.util.Scanner;
      public class Armstrong {
               public static void main(String[] args) {
             
              System.out.println("enter the number to check armstrong");
              Scanner s = new Scanner(System.in);
              int a,b,c,d,e;
              a= s.nextInt();
             b= a%10 ;
             c= a/100 ;
             d= a%100 ;
             e = d / 10;
              if(b*b*b + c*c*c + e*e*e == a)
              {System.out.println("the number is armstrong number");
              } 
               else{
              
              System.out.println("the number is not armstrong");
              }
       }
      }

      Digit Checker program which checks the digit of the number up to 5 digit.

      PROGRAM :

      import javax.swing.JOptionPane;
      public class DigitCheck {
              public static void main(String[] args) {
             
              int a ;
              String s = JOptionPane.showInputDialog("enter  a number");
              a=Integer.parseInt(s);
              if(a>=0 && a<9)
              { JOptionPane.showMessageDialog(null,"one digit number");
              }
              else if (a>9 && a<99){
              JOptionPane.showMessageDialog(null,"two digit number");
              }
              else if (a>99 && a<999){
              JOptionPane.showMessageDialog(null,"three digit number");
              }
              else if (a>999 && a<9999){
              JOptionPane.showMessageDialog(null,"four digit number");
              }
        else if (a>9999 && a<99999){
               JOptionPane.showMessageDialog(null,"five digit number");
              }
              else 
              JOptionPane.showMessageDialog(null,"more than five digit");
          }
      }

      To convert numbers between 100 and 999 into words form.

      PROGRAM :

      import java.util.Scanner;
      public class Conversion {
               public static void main(String[] args)
             {
              int a,c,b,d,e;
              System.out.println("this is the program for conversion of number in words");
              System.out.println("enter a number between 100 and 999");
              Scanner s= new Scanner (System.in);
              a= s.nextInt();
              c= a/100;
              b= a%100;
              d=b/10;
              e = b%10;
              if(c==1)
              System.out.println("One hundred and");
              if(c==2)
              System.out.println("Two hundred and");
               if(c==3)
              System.out.println("Three hundred and");
               if(c==4)
              System.out.println("Four hundred and");
               if(c==5)
              System.out.println("Five hundred and");
               if(c==6)
              System.out.println("Six hundred and");
               if(c==7)
              System.out.println("Seven hundred and");
               if(c==8)
              System.out.println("Eight hundred and");
               if(c==9)
              System.out.println("Nine hundred and");        
              if(b==11)
              System.out.print("Eleven");
               if(b==12)
              System.out.print("Twelve");
              if(b==10)
              System.out.print("Ten");
               if(b==13)
              System.out.print("Thirteen");
               if(b==14)
              System.out.print("Fourteen");
               if(b==15)
              System.out.print("Fifteen");
               if(b==16)
              System.out.print("Sixteen");
               if(b==17)
              System.out.print("Seventeen");
               if(b==18)
              System.out.print("Eighteen");
               if(b==19)
              System.out.print("Nineteen");
               if(b==20 | d==2)
              System.out.print("Twenty");          if(b==30 | d==3)
              System.out.print("Thirty");
               if(b==40 | d==4)
              System.out.print("Forty");
               if(b==50| d==5)
              System.out.print("Fifty");
               if(b==60 |d==6)
              System.out.print("Sixty");
               if(b==70 |d==7)
              System.out.print("Seventy");
               if(b==80 |d==8)
              System.out.print("Eighty");
               if(b==90 | d==9)
              System.out.print("ninety");
              else if(e==1)
              System.out.print("One");
               else if(e==2)
              System.out.print("Two");
               else if(e==3)
              System.out.print("Three");
               else if(e==4)
              System.out.print("Four");
               else if(e==5)
              System.out.print("Five");
               else if(e==6)
              System.out.print("Six");
               else if(e==7)
              System.out.print("Seven");
               else if(e==8)
              System.out.print("Eight");
               else if(e==9)
              System.out.print("Nine");
      }
      }

             
             
             
             
             
             
             
             
             
             
             
             

      Basic Calculator

      PROGRAM :


      import java.util.Scanner;
      class Calculator
      {
      public static void main( String [] args)
      {
      float a,b,c,d,e,f,g;
      System.out.println("Enter the two numbers ");
      Scanner s = new Scanner(System.in);
      a = s.nextFloat();
      b= s.nextFloat();
      System.out.println("the numbers are :"+a  +b);
      c=a+b;
      System.out.println("the addition of two numbers is :"+c);
      d=a-b;
      System.out.println("the subtraction of two numbers is :"+d);
      e=a*b;
      System.out.println("the multiplication of two numbers is :"+e);
      f=a/b;
      System.out.println("the division of two numbers is :"+f);
      g=a%b;
      System.out.println("the modulus of two numbers is :"+g);
      }
      }

      To find area of rectangle

      IN THIS PROGRAM VALUES WILL BE TAKEN  FROM USER

      PROGRAM : 

      1. USING SCANNER :

      import java.util.Scanner;
      class AreaR
      {
      public static void main( String [] args)
      {
      int l,b,c=0;
      System.out.println("Enter the length and breadth of rectangle");
      Scanner s = new Scanner(System.in);
      l = s.nextInt();
      b= s.nextInt();
      System.out.println("the length and breadth given by you  are :"+l +b);
      c= l*b;
      System.out.println("The are of rectangle is:"+c);
      }
      }

      2. USING JAVA SWING

      import javax.swing.*;
      class AreaR
      {
      public static void main (String [] args)
      {
      String s,v ; 
      int l,b,c=0;
      s = JOptionPane.showInputDialog("Enter the length of rectangle");
      l= Integer.parseInt(l);
      v = JOptionPane.showInputDialog("Enter the breadth of rectangle");
      b= Integer.parseInt(v);
      c = l*b;
      JOptionPane.showMessageDialog("The area of rectangle is :"+c);
      }
      }








      To Swap the two numbers using third variable where numbers will be given by user. Using java swing input method

      PROGRAM :

      import javax.swing.*;
      class SwapSwing
      {
      public static void main( String [] args)
      {
      String s,v;
      int a,b,c;
      s= JOptionPane.showInputDialog("Enter the first number");
      a = Integer.parseInt(s);
      v= JOptionPane.showInputDialog("Enter the second number");
      b = Integer.parseInt(v);
      JOptionPane.showMessageDialog("the two number before swapping are :"+a +b);
      c=a;
      a=b;
      b=c;
      JOptionPane.showMessageDialog("the two number after swapping are :"+a +b);
      }
      }

      To Swap the two numbers using third variable where numbers will be given by user. using scanner input method

      PROGRAM :

      import java.util.Scanner;
      class SwapScanner
      {
      public static void main( String [] args)
      {
      int a,b,c=0;
      System.out.println("Enter the two numbers to swap");
      Scanner s = new Scanner(System.in);
      a = s.nextInt();
      b= s.nextInt();
      System.out.println("the numbers before swapping are :"+a  +b);
      c=a;
      a=b;
      b=c;
      System.out.println("the numbers after swapping are:"+a +b);
      }
      }

      To Swap the two numbers using third variable where numbers are already initialized.

      PROGRAM : 

      class Swap
      {
      public static void main( String [] args)
      {
      int a,b,c=0;
      a=15;
      b=50;
      System.out.println("the numbers before swapping are :"+a  +b);
      c=a;
      a=b;
      b=c;
      System.out.println("the numbers after swapping are:"+a +b);
      }
      }