Write a program in Java to input two numbers display the numbers after swapping them by using a third variable.
Sample Input a = 95, b = 45 Sample Output a = 45, b = 95
Answers
Answered by
12
import java.util.Scanner;
public class Swap {
public static void main(String[ ] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers to be swaped - ");
int a = sc.nextInt( ),
b = sc.nextInt( ),
temp = a;
System.out.printf("Before - %n a - %d%n b - %d%n", a, b);
// Swapping numbers
a = b;
b = temp;
System.out.printf("After - %n a - %d%n b - %d%n", a, b);
}
}
Similar questions
Hindi,
5 months ago
Math,
5 months ago
CBSE BOARD X,
10 months ago
Environmental Sciences,
1 year ago
Math,
1 year ago
Math,
1 year ago