
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Swap Two Numbers.
In this article, we will understand how to swap two numbers in Java. This is done using a temporary variable.
Below is a demonstration of the same −
Input
Suppose our input is −
value_1 : 45 value_2 : 70
Output
The desired output would be −
value_1 : 70 value_2 : 45
Algorithm
Step 1- Start Step 2- Declare three integers: value_1, value_2 and temp Step 3- Read the values Step 4- Assign value_1 to temporary variable Step 5- Assign value_2 to value_1 Step 6- Assign temporary variable to value_2 Step 6- Display the two values Step 7- Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.Scanner; public class NumbersSwap{ public static void main(String[] args){ int value_1, value_2, my_temp; System.out.println("The required packages have been imported"); Scanner my_scan = new Scanner(System.in); System.out.println("A scanner object has been defined "); System.out.println("Enter the first number"); value_1 = my_scan.nextInt(); System.out.println("Enter the second number"); value_2 = my_scan.nextInt(); System.out.println("----Before swap----"); System.out.println("The first value is " + value_1 + " and the second value is " + value_2 ); my_temp = value_1; value_1 = value_2; value_2 = my_temp; System.out.println("----After swap----"); System.out.println("The first value is " + value_1 + " and the second value is " + value_2 ); } }
Output
The required packages have been imported A scanner object has been defined Enter the first number 112 Enter the second number 34 ----Before swap---- The first value is 112 and the second value is 34 ----After swap---- The first value is 34 and the second value is 112
Example 2
public class NumbersSwap{ public static void main(String[] args){ int value_1 , value_2, my_temp; System.out.println("The required packages have been imported"); value_1 = 45; value_2 = 70; System.out.println("----Before swap----"); System.out.println("The first number is " + value_1 + " and the second number is " + value_2 ); my_temp = value_1; value_1 = value_2; value_2 = my_temp; System.out.println("----After swap----"); System.out.println("The first number is " + value_1 + " and the second number is " + value_2 ); } }
Output
The required packages have been imported ----Before swap---- The first number is 45 and the second number is 70 ----After swap---- The first number is 70 and the second number is 45
- Related Articles
- Java program to swap two numbers using XOR operator
- C++ Program to Swap Two Numbers
- Haskell Program to Swap Two Numbers
- Kotlin Program to Swap Two Numbers
- Java program to swap two integers
- 8085 program to swap two 8-bit numbers
- How to Swap Two Numbers in Swift Program?
- Swap two numbers in C#
- How to Swap Two Numbers in Golang?
- 8085 program to swap two 8 bit numbers using Direct addressing mode
- 8085 program to swap two 16-bit numbers using Direct addressing mode
- C program to swap two strings
- Write a Golang program to swap two numbers without using a third variable
- Java Program to Add Two Numbers
- Java Program to Add the two Numbers

Advertisements