Computer Science, asked by ali964144, 1 year ago

write a java program to input two unequal numbers.Display the numbers after swapping their values in the variables without using a third value.​

Answers

Answered by jhasumit
3

hi mate♥️♥️♥️♥️

public class SwapElementsWithoutThirdVariableExample {

public static void main(String[] args) {

int num1 = 10;

int num2 = 20;

System.out.println("Before Swapping");

System.out.println("Value of num1 is :" + num1);

System.out.println("Value of num2 is :" +num2);

//add both the numbers and assign it to first

num1 = num1 + num2;

num2 = num1 - num2;

num1 = num1 - num2;

System.out.println("Before Swapping");

System.out.println("Value of num1 is :" + num1);

System.out.println("Value of num2 is :" +num2);

}

}

/*

Output of Swap Numbers Without Using Third Variable example would be

Before Swapping

Value of num1 is :10

Value of num2 is :20

Before Swapping

Value of num1 is :20

Value of num2 is :10

*/

Answered by User0123
3

Hey mate.....

Here's your answer.....

import java.util.*;

class swap

{

int a,b;

Scanner s=new Scanner (System.in);

void in( )

{

System.out.println ("Enter 2 numbers ");

a=s.nextInt( );

b=s.nextInt( );

}

void process ( )

{

a=a+b;

b=a-b;

a=a-b;

}

void output ( )

{

System.out.println (" After swapping,value of 1st no.="+b+"and value of 2nd no."+a);

}

public static void main (String args[ ])

{

swap z=new swap( );

z.in( );

z.process( );

z.output( );

}

}

Thanks...

Hope it helps.....

Similar questions