Computer Science, asked by AabhashBhowmick, 3 months ago

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​

Answers

Answered by JOkeEvil
2

C0DE :

import java.util.*;

class Swapping

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.println("Enter 2 numbers");

int a=sc.nextInt();

int b=sc.nextInt();

a=a+b;

b=a-b;

a=a-b;

System.out.println("Swapped value of a :"+b);

System.out.println("Swapped value of b :"+a);

}

}

LOGIC :

First note the step :

a = a + b

Then b = a - b

= > b = a + b - b [ from above ]

= > b = a

then a = a - b

= > a = a  - (  a - b )

= > a = a - a + b

= > a = b

That is the way they are swapped !

Answered by pinky22267
4

Answer:

✨✌HOPE IT HELPS YOU✌✨

Explanation:

import java.util.Scanner;

public class bloat number swap

{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);

Similar questions