12:23 am - Tuesday May 22, 2018

Java Example Program to swap numbers without using third or temp variable

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
/* java program for swapping of two numbers without using third variable*/
/**
*@author:Candidjava.com
*@Descripton:swapping of two numbers without third variable
*/
public class Swap {
	public static void main(String[] args) {
		int a = 10;
		int b = 5;
		System.out.println("Before swapping:"); // display message
		System.out.println(a);
		System.out.println(b);
		a = a + b;
		b = a - b;
		a = a - b;
		System.out.println("After Swapping:");
		System.out.println(a);
		System.out.println(b);
	}
}

Output:

Before swapping:
a=10
b=5
After swapping:
a=5
b=10

Filed in: Sample Programs in java

Comments are closed.