Java Program to Swap Two Numbers
Java Program to Swap Two Numbers
To swap two numbers means to interchange the data of two variables. We have used a third variable to do this interchange or swaping.For eg, Before swap: a=1 b=2
After swap: a=2 b=1
-------------------------------------------------------------------------------------------------
import java. util.*;
class Interchange
{ public static void
main()
{ Scanner Sc=new
Scanner(System.in);
System.out.println("Enter two numbers :");
int c=0;
int a=Sc.nextInt();
int b=Sc.nextInt();
{ System.out.println("The
two integers are "+a+" "+b);
}
{ c=a;
a=b;
b=c;
{ System.out.println("The interchanged
integers are "+a+" "+b); }
} } }
Output: Enter two numbers
:
1
2
The two integers are 1 2
The
interchanged integers are 2 1
Java Program to Swap Two Numbers
--------------------------------------------------------------------------------------------
Just copy paste the code into the class file of Bluej compiler. Change the class name according to your's.
Thanks for visting Programming in JAVA
Happy Coding !!
No comments: