Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Swap two number in java using the third Variable
Ans:
In this tutorial you will learn swapping of two numbers using third variable Java in programming language.
This is also a most important program which commonly asked in interview.
Before directly moving on writing the program lets understand what is our aim to achieve in this program.
Basically Swapping of number means
Suppose variable a has assigned value is 2 and the variable b has assigned value 4 then in swapping operation we will exchange the value and assign value of a i.e 2 in b and the value of b i.e. 4 in variable a.
How our program will behave?
As we already seen above that what we have to achieve in the program.
In the swapping program without using third variable we will assign two different value to the different variables.
For example: a=2 and b=4
Now after execution of the program our output should like
a=4 and b = 2
Swapping of two numbers using third variable in Java language
import java.util.*;
class Main{
public static void main(String ...args){
int tempvar;
Scanner sc= new Scanner(System.in);
System.out.print("Enter first number- ");
int firstno= sc.nextInt();
System.out.print("Enter second number- ");
int secondno= sc.nextInt();
System.out.print("before swapping number are : "+firstno+" and "+secondno);
tempvar=firstno;
firstno=secondno;
secondno=tempvar;
System.out.print("\nafter swapping number are : "+firstno+" and "+secondno);
}
}
Output:
Also prepare these Java Programs:
- Java Program to print Fibonacci series using recursive method
- Palindrome program in Java for number using Recursive method
- Java program to check a given is an Armstrong
- Java program to add two numbers without using addition operator
- How to remove a character from a string in java
- Write a program in Java to count occurrence of a given character in a String.
- Java program to find largest and smallest number in an array
- Java program to print first duplicate number in an array of 1-100
- Java Program to remove duplicate elements from an array
- Program to count repeated elements in an array java
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program