Java Program To Swap Two numbers With Third Variable In Java | Java Practice Programs| Code Bode by CodeBode

226 Views - 5 years ago

CodeBode

3.54 K Subscribers

#swapnumbers #javatutorial #javaprograms

Hi Guyz,
In This Video We will learn how to swap two numbers in java with a third variable.

import java.util.Scanner;

public class NumberSwap {

public static void main(String[] args) {
// Swap two numbers using third variable
// 1. Take two input variables
// 2. Take a temp variable
// 3. assign number1 to temp
// 4. assign number2 to number1
// 5. assign temp to number2
// 6. Print two numbers

Scanner sc = new Scanner(System.in);
System.out.println("Please enter number 1");
int number1=sc.nextInt(); //10
System.out.println("Please enter number 2");
int number2=sc.nextInt(); //20
int temp;

temp=number1; //10
number1=number2; //20
number2=temp; //10

System.out.println("Number 1 :" +number1);
System.out.println("Number 2: "+number2);

}

}

Other Playlist :

Eclipse Tutorial :
https://www.youtube.com/playlist?list=PLQH2XCVoqus2AmSJqpVOoZWizsXtjcaHO
HTML Tutorial :
https://www.youtube.com/playlist?list=PLQH2XCVoqus3wQNcNN_QjOGNFj7Ih4w8g
Java Learning Path :
HAPPY LEARNING!

Related Videos