Saved Bookmarks
| 1. |
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 |
|
Answer» 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); } } |
|