Computer Science, asked by shreyamehrotra30, 17 hours ago

WAP TO Swap two values. The numbers are 25, 50.

Answers

Answered by ziankabir
0

Answer:

Example: Suppose, there are two numbers 25 and 50.

Let

X= 25 (First number), Y= 50 (second number)

Swapping Logic:

X = X + Y = 25 +50 = 75

Y = X - Y = 75 - 25 = 50

X = X -Y = 50 - 25 = 25

and the numbers are swapped as X =50 and Y =25.  

Algorithm

STEP 1: START

STEP 2: ENTER x, y

STEP 3: PRINT x, y

STEP 4: x = x + y

STEP 5: y= x - y

STEP 6: x =x - y

STEP 7: PRINT x, y

STEP 8: END

Java Program

import java.util.*;  

class Swap    

{  

   public static void main(String a[])    

   {    

       System.out.println("Enter the value of x and y");  

       Scanner sc = new Scanner(System.in);  

       /*Define variables*/  

       int x = sc.nextInt();  

       int y = sc.nextInt();  

       System.out.println("before swapping numbers: "+x +" "+ y);  

      /*Swapping*/  

       x = x + y;    

       y = x - y;    

       x = x - y;    

       System.out.println("After swapping: "+x +"  " + y);    

   }    

}  

Explanation:

Similar questions