Sunday, September 4, 2016

Java Program to Swap Numbers without using 3rd variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;

public class Swapping{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the 1st number: ");
int x = in.nextInt();
System.out.println("Enter the 2nd number: ");
int y = in.nextInt();

System.out.println("Initial value of x: "+x+" and y: "+y);

x = x+y;
y = x-y;
x = x-y;

System.out.println("After swapping value of x: "+x+" and y: "+y);
}
}


Output

1
2
3
4
5
6
Enter the 1st number:
 43
 Enter the 2nd number:
 56
 Initial value of x: 43 and y: 56
 After swapping value of x: 56 and y: 43

Bhanu Namikaze

Author & Editor

Bhanu Namikaze is an Ethical Hacker, Passionate Blogger, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors, Surfing and Social Networking. Feel Free To Contact Me @ Bhanu.

0 comments:

Post a Comment

Popular Posts

Copyrights @ Interview Questions.Me - Blogger Templates Designed by Templateism - Distributed By Protemplateslab

  • (91) 5544 654942
  • Bhanu@HackingDream.net
  • Interview Questions

Copyrights @ Interview Questions.Me -A Site By Bhanu In Association With Hacking Dream