Java Program to Swap Two Numbers

0

 


The idea behind this code is to use arithmetic operators. First of all We take the sum of both the numbers and store it in first number and store the difference of both the numbers in the second number. Now we will store the difference of both the numbers in the first number. Now the values will be updated after the first two operations and the values are swapped. 



public class HindiCodingCommunity {

public static void main(String[] args) {

int first = 2, second = 5;

System.out.println("First number is = " + first);
System.out.println("Second number is = " + second);

first = first - second;
second = first + second;
first = second - first;

System.out.println("After Swapping");
System.out.println("First number is = " + first);
System.out.println("Second number is = " + second);
}
}








Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !