Swap two numbers using third variable in java?

Swap two numbers using third variable:

Write a program to exchange or swap two numbers. We should use any temporary or third variable to swap.

import java.util.Scanner;

class SwapTwoNumbers
{
public static void main(String args[])
{
int c1,c2 , temp;
Scanner j=new Scanner(System.in);
System.out.print(“enter the c1 value : “);
c1=j.nextInt();
System.out.print(“enter the c2 value : “);
c2=j.nextInt();
System.out.println(“Before swapping c1 value: “+c1);
System.out.println(“Before swapping c2 value: “+c2);
temp = c1;
c1 = c2;
c2 = temp;
System.out.println(“c1 value After swapping c1 value: “+c1);
System.out.println(“c2 value After swapping c2 value: “+c2);
}
}

Output:

Enter the c1 value : 10
Enter the c2 value ; 9
c1 value After swapping : 9
c2 value After swapping: 10

Related Programs Below:
Write a program the given number is even or odd in java?
Write a program for the given number is Fibonacci or not in java?
Prime numbers in Java?
Palindrome number Program in java?
Armstrong Number or not in Java?
Write a program for the given number is strong Number or Not  in java?
Write a program swapping two numbers without using third variable in java ?
Swapping of two numbers using third variable in java?

Leave a Reply

Your email address will not be published.