๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

WAP to swap two numbers in Java  


Logical Steps (Using Third Variable):
  • To swap two numbers using third variable, follow these steps:
    1. Take two numbers as an input from user.
    2. Use two integer variables (no1 and no2) to hold the input values.
    3. Declare a third variable (temp) to temporarily store the value of one variable during the swap.
    4. Swap the Values:
      • Assign the value of no1 to temp.
      • Assign the value of no1 to no1.
      • Assign the value of temp to no2.
    5. Print the values of no1 and no2 after swapping.
Programs:
  • Below is the simple program:
    public class SwapNumbers
    {
        public static void main(String[] args)
        {
            int no1=22, no2=33;
    
            // Display numbers before swapping
            System.out.println("Before swapping: no1 = " + no1 + ", no2 = " + no2);
    
            // Swap using a third variable
            int temp = no1;
            no1 = no2;
            no2 = temp;
    
            // Display numbers after swapping
            System.out.println("After swapping: no1 = " + no1 + ", no2 = " + no2);
        }
    }
    Output:
    Before swapping: a = 22, b = 33
    After swapping: a = 33, b = 22
  • Below is the program by taking user input:
    import java.util.Scanner;
    
    public class SwapNumbers
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
    
            // Take input from the user
            System.out.print("Enter the first number (no1): ");
            int no1 = scanner.nextInt();
    
            System.out.print("Enter the second number (no2): ");
            int no2 = scanner.nextInt();
    
            // Display numbers before swapping
            System.out.println("Before swapping: no1 = " + no1 + ", no2 = " + no2);
    
            // Swap using a third variable
            int temp = no1;
            no1 = no2;
            no2 = temp;
    
            // Display numbers after swapping
            System.out.println("After swapping: no1 = " + no1 + ", no2 = " + no2);
    
            // Close the scanner
            scanner.close();
        }
    }
    Output:
    Enter the first number (a): 100
    Enter the second number (b): 200
    Before swapping: a = 100, b = 200
    After swapping: a = 200, b = 100

Logical Steps (Without Using Third Variable):
  • To find the factorial of a number, follow these steps:
    1. Take two numbers as an input from user.
    2. Use two integer variables (no1 and no2) to hold the input values.
    3. Use arithmetic operations to swap the values:
      • a = a + b
      • b = a - b
      • a = a - b
    4. Print the values of no1 and no2 after swapping.
Programs:
  • Below is the simple program:
    public class SwapNumbers2
    {
        public static void main(String[] args)
        {
            int no1=33, no2=44;
    
            // Display numbers before swapping
            System.out.println("Before swapping: no1 = " + no1 + ", no2 = " + no2);
    
            // Swap without using a third variable
            no1 = no1 + no2;
            no2 = no1 - no2;
            no1 = no1 - no2;
    
            // Display numbers after swapping
            System.out.println("After swapping: no1 = " + no1 + ", no2 = " + no2);
        }
    }
    Output:
    Before swapping: no1 = 33, no2 = 44
    After swapping: no1 = 44, no2 = 33
  • Below is the program by taking user input:
    import java.util.Scanner;
    
    public class SwapNumbers2
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
    
            // Take input from the user
            System.out.print("Enter the first number (no1): ");
            int no1 = scanner.nextInt();
    
            System.out.print("Enter the second number (no1): ");
            int no2 = scanner.nextInt();
    
            // Display numbers before swapping
            System.out.println("Before swapping: no1 = " + no1 + ", no2 = " + no2);
    
            // Swap without using a third variable
            no1 = no1 + no2;
            no2 = no1 - no2;
            no1 = no1 - no2;
    
            // Display numbers after swapping
            System.out.println("After swapping: no1 = " + no1 + ", no2 = " + no2);
    
            // Close the scanner
            scanner.close();
        }
    }
    Output:
    Enter the first number (no1): 555
    Enter the second number (no1): 888
    Before swapping: no1 = 555, no2 = 888
    After swapping: no1 = 888, no2 = 555