Write a program to accept two integer numbers ( in two variables x and y
respectively ) and swap their values
i. Using a third variable.
ii. Without using a third variable.
Display the values of the variables before and after change.
Answers
Answer:
Hey this is the program for your question using third variable..
import java.util.Scanner;
public class main{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int a, b,c;
a=in.nextInt();
b=in.nextInt();
c=a;
a=b;
b=c;
System.out.println(a);
System.out.println(b);
}
}
This is the answer without using third variable..
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:
Please mark my answer as Brainliest and also follow me for more answers like this...
Thank You..