-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapTwoNumbers.java
More file actions
31 lines (24 loc) · 899 Bytes
/
SwapTwoNumbers.java
File metadata and controls
31 lines (24 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// program to swap to numbers without using third variable
import java.util.Scanner;
class SwapTwoNumbers
{
public static void main(String args[])
{
int First,Second;
// display msg to get input from screen
System.out.println("enter the first and second number");
//create object of Scanner class
Scanner in = new Scanner(System.in);
// take input by using nextInt method
First = in.nextInt();
Second = in.nextInt();
//before swaping
System.out.println("before swaping first number is "+ First +" second number is "+ Second);
// After swaping a=10,b=20 a=a+b
First = First+Second;// a=30
Second = First-Second;// b=30-20 b=10
First = First-Second; // a=a-b a=30-10=20
//display the swaped numbers
System.out.println("After Swaping first number is "+ First +" second number is "+ Second);
}
}