This is a Java Program to Read Two Integers M and N & Swap their Values.
Enter any two integer numbers as input. After that we take a new variable temp and copy first variable to this temp variable. Now we copy second variable to first variable and temp variable into second variable. Hence we get the swapped values as output.
Here is the source code of the Java Program to Read Two Integers M and N & Swap their Values. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
advertisement
import java.util.Scanner;
public class Swap_Integers
{
public static void main(String args[])
{
int m, n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
m = s.nextInt();
System.out.print("Enter the second number:");
n = s.nextInt();
temp = m;
m = n;
n = temp;
System.out.println("After Swapping");
System.out.println("First number:"+m);
System.out.println("Second number:"+n);
}
}
Output:
$ javac Swap_Integers.java $ java Swap_Integers Enter the first number:5 Enter the second number:7 After Swapping First number:7 Second number:5
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.