In this article we are going to discuss a problem; this is one of the most commonly asked interview question of today. Write a program to swap two numbers without using a temporary variable.
Write a program to swap two numbers without using a temporary variable.
e.g. if int a=10 and int b=20 are two numbers than we have to swap their values to, a=20 and b=10 and using a and b only.
package com.tbNext.byexample; public class TBSwapTwoNumbersWithoutTemp { /* * Swapping two variables without using a temp variable */ public static void main(String[] args) { int a = 4; int b = 10; System.out.println("Before Swapping a: " + a + " b:" + b); a = a + b; b = a - b; a = a - b; System.out.println("After Swapping a: " + a + " b:" + b); } }
2014-2017 © Techburps. ALL Rights Reserved. Disclaimer Contact Us