Write a java programs for swapping of two numbers using third variable.
Answers
Answer:
import java.util.*;
class Swap_With {
public static void main(String[] args) {
int x, y, t;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of X and Y");
x = sc.nextInt();
y = sc.nextInt();
System.out.println("before swapping numbers: "+x +" "+ y);
t = x;
x = y;
y = t;
System.out.println("After swapping: "+x +" " + y);
System.out.println( );
}
}
Explanation:
This will accept two numbers and then will swap the numbers
Question:-
Write a Java program to swap two numbers using third variable.
Program:-
import java.util.*;
class Swap
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a=sc.nextInt();
System.out.println("Enter b: ");
int b=sc.nextInt();
System.out.println("Before Swapping: ");
System.out.println("a: "+a);
System.out.println(" b: "+b);
int c=a;
a=b;
b=c;
System.out.println("After Swapping: ");
System.out.println("a: "+a);
System.out.println(" b: "+b);
/*
*Written by
* @Anindya Adhikari
*/
}
}