Computer Science, asked by gauri72, 1 year ago

Write a Java program to interchange the value of two numbers without using the third variable.

Answers

Answered by NeneAmaano
28
int java.io.*;
int java.lang.*;
public class MySwapingTwoNumbers
{ public static void main(String a[])
{ int x = 10;
int y = 20;
System.out.println("Before swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
x = x+y;
y=x-y;
x=x-y;
System.out.println("After swap:");
System.out.println("x value: "+x);
System.out.println("y value: "+y);
}
}
Answered by upadhyayyashika29
6

Answer:

#include<stdio.h>  

int main()    

{    

int a=10, b=20;      

printf("Before swap a=%d b=%d",a,b);      

a=a+b;//a=30 (10+20)    

b=a-b;//b=10 (30-20)    

a=a-b;//a=20 (30-10)    

printf("\nAfter swap a=%d b=%d",a,b);    

return 0;  

}

Explanation:

hope its helpful.........

Similar questions
Math, 7 months ago