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
Category: Basic Programs Java Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments