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

231 Likes

Answer

import java.util.Scanner;

public class KboatNumberSwap
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter two unequal numbers");
        System.out.print("Enter first number: ");
        int a = in.nextInt();
        System.out.print("Enter second number: ");
        int b = in.nextInt();
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println("a = " + a + " b = " + b);
    }
}

Variable Description Table

Program Explanation

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 = 23

Answered By

93 Likes


Related Questions