KnowledgeBoat Logo

Computer Applications

Write a program to input two unequal numbers. Display the numbers after swapping their values in the variables without using a third variable.
Sample Input: a=23, b= 56
Sample Output: a=56, b=23

Java

Input in Java

30 Likes

Answer

import java.io.*;

public class KboatNumberSwap
{
    public static void main(String args[]) throws IOException {
        InputStreamReader read = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(read);
        
        System.out.print("Enter first number: ");
        int a = Integer.parseInt(in.readLine());
        
        System.out.print("Enter second number: ");
        int b = Integer.parseInt(in.readLine());
        
        a = a + b;
        b = a - b;
        a = a - b;
        
        System.out.println("Numbers after Swap:");
        System.out.println("a = " + a + "\t" + "b = " + b);
    }
}

Output

BlueJ output of Write a program to input two unequal numbers. Display the numbers after swapping their values in the variables without using a third variable. Sample Input: a=23, b= 56 Sample Output: a=56, b=23BlueJ output of Write a program to input two unequal numbers. Display the numbers after swapping their values in the variables without using a third variable. Sample Input: a=23, b= 56 Sample Output: a=56, b=23

Answered By

15 Likes


Related Questions