In this short tutorial I will show you how to swap two numbers using Java as our programming language. What does our program do is very simple it will ask the user to enter two numbers and then our program will display the original arrangement of the two numbers and after that it will display the swap of arrangements of the two numbers. I've written a method of how to interchange the arrangement of the two numbers. I hope you will find my work useful and beneficial in your programming assignments and projects using Java.
If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.
People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360.
Program Listing
import java.util.Scanner;
class swap
{
public static int swap_values(int x,int y)
{
int temp=0;
temp = x;
x = y;
y = temp;
System.out.println("\n");
System.out.println("====== SWAP VALUES ======");
System.out.println();
System.out.println(" 1ST VALUE :=> " + x);
System.out.println(" 2ND VALUE :=> " + y);
return 0;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
char reply;
do
{
int a, b;
System.out.print("\n");
System.out.println("==============================================");
System.out.println("|| <<< SWAP OF TWO NUMBERS >>> ||");
System.out.println("==============================================");
System.out.print("\n");
System.out.print("ENTER FIRST VALUE :=> ");
a = in.nextInt();
System.out.print("ENTER SECOND VALUE :=> ");
b = in.nextInt();
System.out.println();
System.out.println("====== ORIGINAL VALUES ======");
System.out.println();
System.out.println(" 1ST VALUE :=> " + a);
System.out.println(" 2ND VALUE :=> " + b);
swap_values(a,b);
System.out.println("\n\n");
System.out.print("Do you Want To Continue (Y/N) :=> ");
reply=in.next().charAt(0);
} while(reply=='Y'|| reply=='y');
System.out.println("\n");
System.out.println("\t ===== END OF PROGRAM ======");
System.out.println("\n");
}
}
No comments:
Post a Comment