Example of swaping of two numbers without using third variable in java?
Answers
Answered by
1
hey..I'll tell u logic
a=a+b
b=a-b
a=a-b
nw write acc to the Java's syntax
a=a+b
b=a-b
a=a-b
nw write acc to the Java's syntax
vershamishra12oycd27:
mark it as brainliest if it helps u
Answered by
0
package com.company;
/* 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
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number a:");
int a = sc.nextInt();
System.out.println("Enter Number b:");
int b = sc.nextInt();
if (a!=b){
a = a+b;
b = a-b;
a = a-b;
}
System.out.println("a = "+a+" b = "+b);
}
}
Similar questions
English,
7 months ago
Computer Science,
7 months ago
Math,
1 year ago
Math,
1 year ago
Hindi,
1 year ago