Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scratchyourbrain/5e3edfbd40a45edf5b27 to your computer and use it in GitHub Desktop.
Save scratchyourbrain/5e3edfbd40a45edf5b27 to your computer and use it in GitHub Desktop.
Java Program to Swap Two Numbers
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);
}
}
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