Created
January 21, 2015 14:05
-
-
Save scratchyourbrain/5e3edfbd40a45edf5b27 to your computer and use it in GitHub Desktop.
Java Program to Swap Two Numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
class swap | |
{ | |
public static void main(String args[]) | |
{ | |
Scanner s = new Scanner(System.in); | |
System.out.print("Enter two numbers : "); | |
int m = s.nextInt(); | |
int n = s.nextInt(); | |
int temp; | |
System.out.println("BEFORE SWAP"); | |
System.out.println("m = " + m + " n = " + n); | |
temp = n; | |
n = m; | |
m = temp; | |
System.out.println("AFTER SWAP"); | |
System.out.println("m = " + m + " n = " + n); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
class swap | |
{ | |
public static void main(String args[]) | |
{ | |
Scanner s = new Scanner(System.in); | |
System.out.print("Enter two numbers : "); | |
int m = s.nextInt(); | |
int n = s.nextInt(); | |
int temp; | |
System.out.println("BEFORE SWAP"); | |
System.out.println("m = " + m + " n = " + n); | |
m = m + n; | |
n = m - n; | |
m = m - n; | |
System.out.println("AFTER SWAP"); | |
System.out.println("m = " + m + " n = " + n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment