Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 8, 2019

How to SWAP Two Numbers in Java Without Temp variable or Without Third variable





In this blog, we will see How to SWAP Two Numbers in Java without Temp Variable or without third variable. This will be asked in the most of the interview questions.


In the below snippet, we have done the following to swap two numbers without using temp variable:

  1. Create two int variables and assign them with some values. Here we have taken a=20 and b=30 respectively.
  2. Here, swap operation can be done by assigning the values of variables (a and b) to one another as below and getting the latest values in those respective variables which is shown in the below code:


package Javaprograms;

public class SwapTwoNumbersWithoutTempVariable {

	public static void main(String[] args) {
		int a = 20;
		int b = 30;
		
		System.out.println("a value before swapping is:" +a);
		System.out.println("b value before swapping is:" +b);
		
		System.out.println("---------------------------------");
		a = a+b; // a=20 + b=30; now a value = 50
		b = a-b; // a=50 - b=30; now b value = 20
		a = a-b; // a=50 - b=20; now a value = 30
		
		System.out.println("a value after swapping is:" +a);
		System.out.println("b value after swapping is:" +b);
		
	}

}
Output:
--------
a value before swapping is:20
b value before swapping is:30
---------------------------------
a value after swapping is:30
b value after swapping is:20





Please watch the Youtube video for better understanding.



Share this post: on Twitter on Facebook

How to SWAP Two Numbers in Java using Temp Variable How to Capture WebElement Screenshot in Selenium Webdriver using selenium 4

Related Posts

How to Swap Two Numbers in Java with Temp Variable

Java IQs

How to SWAP Two Numbers in Java using Temp Variable

How to Read Properties file in Java

Java IQs, Uncategorized

How to Read Data From Properties File in Java

Compare two arrays in java with out inbuilt functions

Java IQs

How to Compare Two Arrays in Java without built-in functions

Compare two arrays in java

Java IQs

How to Compare Two Arrays in Java using built-in functions

Remove White Spaces From A String in Java

Java IQs

How to Remove Whitespaces from a String in Java

Sum of The digits in a number in Java

Java IQs

How to Perform Sum of the Digits in a Given Number in Java

How To Reverse A String in Java

Java IQs

How to Reverse a String in Java

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025