Write a Java program to Swap two numbers using third variable

By | 18.01.2017

Write a Java program to Swap two numbers using third variable

java-basic-programs

 

Java program to swap two numbers:

Swapping is the process of exchange the values of two variables with each other. For example variable num1 contains 1 and num2 contains 2 after swap their values are num1 contains 2 and num2 contains 1.

SOURCE CODE::

import java.util.*;
public class Swapnumber 
{
 public static void main(String[] args) 
 {
  int a, b, c;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter Value in a :");
  a=s.nextInt();
  System.out.println("Enter Value in b :");
  b=s.nextInt();
  c=a;
  a=b;
  b=c;
  System.out.println("Values in a:" +a);
  System.out.println("Values in b:" +b);
 }
}

 

OUTPUT ::

 

Enter Value in a : 1
Enter Value in b : 2
Value in a : 2
Value in b : 1

 

 

4.3 3 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments