Program to Swap Two Numbers using third variable as well without third variable (Java)

Java Programs
So today we are going to write program that accepts two numbers from the user and swap the numbers using third variable as without using third variable. So this is a very simple program and it does not require much logic to write this program.
So lets get started...


Swapping using Third Variable

Q. Write a program to accept two numbers from the user and swap them using the third variable.
For Example: Input: N = 5, M = 10; Output: N = 10, M = 5

Solution:
 import java.util.*;
 class SwapNumber_USingThirdVariable
 {
     public static void main(String[] args)
     {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter first number: ");
         int n = sc.nextInt();
         System.out.println("Enter second number: ");
         int m = sc.nextInt();
         System.out.println("Before Swap: ");
         System.out.println("N = " + n);
         System.out.println("M = " + m);
         int swap;
         swap = n; // Here Swapping Starts.
         n = m;
         m = swap;
         System.out.println("After Swap: ");
         System.out.println("N = " + n);
         System.out.println("M = " + m);
     }
 }

Code Written in BlueJ

Output:
Enter first number: 
5
Enter second number: 
10
Before Swap: 
N = 5
M = 10
After Swap: 
N = 10
M = 5

Code Written in BlueJ

Variable description used in program:
int n - It is used to accept a number from the user.
int m - It is used to accept a number from the user.
int swap - It is used to swap the numbers.
Explanation:
In this program first we have accepted two numbers from the user i.e., "int n" and "int m". Then we have created a new variable i.e., "int swap", to swap the two numbers. Then we have given value of "n" to " swap" and value of "m" to "n" and at last the value of "swap"(which is actually the earlier value of "n") to "m". And this is how the number are swapped using the third variable. Here is the flowchart that shows how it works:


Swapping without using Third Variable

Q. Write a program to accept two numbers from the user and swap them using the third variable.
For Example: Input: N = 5, M = 10; Output: N = 10, M = 5

Solution:
 import java.util.*;
 class SwapNumber_WithoutUSingThirdVariable
 {
     public static void main(String[] args)
     {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter first number: ");
         int n = sc.nextInt();
         System.out.println("Enter second number: ");
         int m = sc.nextInt();
         System.out.println("Before Swap: ");
         System.out.println("N = " + n);
         System.out.println("M = " + m);
         n = m+n;
         m = n-m;
         n = n-m;
         System.out.println("After Swap: ");
         System.out.println("N = " + n);
         System.out.println("M = " + m);
     }
 }

Code Written in BlueJ

Output:
Enter first number: 
5
Enter second number: 
10
Before Swap: 
N = 5
M = 10
After Swap: 
N = 10
M = 5

Output in BlueJ

Variable description used in program:
int n - It is used to accept a number from the user.
int m - It is used to accept a number from the user.
Explanation:
In this program first we have accepted two numbers from the user i.e., "int n" and "int m". Then we have have swapped the both numbers with using third variable, as in "n = m+n" we have added the variable and then we "m = n-m" subtracted and again we subtracted "n=m-n". Here is the flowchart how this program works:


So that is it to for Swapping Two Numbers. Hope you find it useful and if you have any query do comment in the comment box and I will try to solve your query. Will see you on next program.

Thank You.
Program to Swap Two Numbers using third variable as well without third variable (Java) Program to Swap Two Numbers using third variable as well without third variable (Java) Reviewed by Get2Know on October 09, 2020 Rating: 5

No comments:

Business

Powered by Blogger.