Java Programming

  1. Java Program to Swap Two Numbers using third variable
    
    public class SwapNumbers {
    
      public static void main(String[] args) {
                  
      int first = 30, second = 40;
                  
      System.out.println("--Before swap--");
      System.out.println("First number = " + first);
      System.out.println("Second number = " + second);
                        
      int temp = first;                  
      first = second;                 
      second = temp;
                  
      System.out.println("--After swap--");
      System.out.println("First number = " + first);
      System.out.println("Second number = " + second);
          }
        }
  2. Output:
    
     --Before swap--
    First number = 30
    Second number = 40
    --After swap--
     First number = 40            
    Second number = 30
                  
  3. Java Program to Swap Two Numbers without third variable
    
    public class SwapNumbers {
    
    public static void main(String[] args) {
                
    int first = 30, second = 40;
                
    System.out.println("--Before swap--");
    System.out.println("First number = " + first);
    System.out.println("Second number = " + second);
                     
    first = first + second;   
    second = first - second;   
    first = first - second;
                
    System.out.println("--After swap--");
    System.out.println("First number = " + first);
    System.out.println("Second number = " + second);
        }
      }
  4. Output:
    
     --Before swap--
    First number = 30
    Second number = 40
    --After swap--
    First number = 40            
    Second number = 30
                
  5. Java Program to Swap Two Numbers with third variable using user input
    
    import java.util.*
    
    public class SwapNumbers {
    
    public static void main(String[] args) {
              
    System.out.println("Enter the value of first and second no");  
    Scanner sc = new Scanner(System.in);  
    int first = sc.nextInt();  
    int second = sc.nextInt();
              
    System.out.println("--Before swap--");
    System.out.println("First number = " + first);
    System.out.println("Second number = " + second);
              
    int temp = first;                  
    first = second;              
    second = temp; 
            
    System.out.println("--After swap--");
    System.out.println("First number = " + first);
    System.out.println("Second number = " + second);
      }
    }
  6. Output:
    
    Enter the value of first and second no 30 40
    --Before swap--
    First number = 30
    Second number = 40
    --After swap--
    First number = 40            
    Second number = 30
              
  7. Java Program to Swap Two Numbers with third variable using user input
    
    import java.util.*
    public class SwapNumbers {
    
    public static void main(String[] args) {
              
    System.out.println("Enter the value of first and second no");  
    Scanner sc = new Scanner(System.in);  
    int first = sc.nextInt();  
    int second = sc.nextInt();
              
    System.out.println("--Before swap--");
    System.out.println("First number = " + first);
    System.out.println("Second number = " + second);
              
                      
    first = first + second;   
    second = first - second;   
    first = first - second;
              
    System.out.println("--After swap--");
    System.out.println("First number = " + first);
    System.out.println("Second number = " + second);
      }
    }
  8. Output:
    
    Enter the value of first and second no 30 40
    --Before swap--
    First number = 30
    Second number = 40
    --After swap--
    First number = 40            
    Second number = 30
              

Others Programming Language

article

programming C

Muktarul Hossain
article

C++

Muktarul Hossain
article

Python

Muktarul Hossain
article

Website Design

Muktarul Hossain