Swap two numbers without using third variable
In this post, you will learn how to swap two numbers without using the third variable in different programming languages.
Algorithm
STEP 1: START
STEP 2: DEFINE x, y
STEP 3: ENTER x, y
STEP 4: PRINT x, y
STEP 5: x = x + y
STEP 6: y = x - y
STEP 7: x = x - y
STEP 8: PRINT x, y
STEP 9: END
Java program to swap two numbers
In the given Java program, we swap two numbers without using the temporary variable. We define only two variables. We use any one variable from a given two variables to store the sum of both variables. Then, we use the subtraction operation to get the values swapped.
// Swapping of two numbers
public class SwapNumbers
{
public static void main(String[] args)
{
int x = 500;
int y = 400;
// Displaying values
System.out.println("x = " + x + " and y = " + y);
//Swapping in steps
x = x + y; //x = 500 + 400 = 900
y = x - y; //y = 500 - 400 = 100
x = x - y; //x = 500 - 400 = 100
//Verifing swapped values
System.out.println("x = " + x + " and y = " + y);
}
}
Output of the above code:
x = 500 and y = 400
x = 400 and y = 500
C program to swap two numbers using + and -
In the given C program, we ask the user to enter two integer values and store them in two variables. Then, we swap the entered value using the addition and subtraction operators.
#include<stdio.h>
int main()
{
int x, y;
printf("Enter the value of x and y: ");
scanf("%d %d",&x,&y);
printf("Before swapping: %d %d\n",x,y);
/*Swapping in steps */
x = x + y;
y = x - y;
x = x - y;
/*Verifing swapped values */
printf("After swapping: %d %d",x,y);
return 0;
}
Output of the above code:
Enter the value of x and y: 200 150
Before swapping: 200 150
After swapping: 150 200
C program to swap two numbers using * and /
In the given C program, we swap two numbers using * and / operators.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=30, b=50;
printf("Before swap a=%d b=%d",a,b);
a=a*b;//a=1500 (30*50)
b=a/b;//b=30 (1500/50)
a=a/b;//a=50 (1500/30)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
Output of the above code:
Before swap a=30 b=50
After swap a=50 b=30
C program to swap two numbers using Bitwise XOR
Here, we have used the bitwise XOR operator to swap two variables. The XOR of two numbers x and y returns a number that has all the bits as 1 wherever bits of x and y differ.
#include <stdio.h>
int main()
{
int x = 40, y = 55;
printf("Before swapping: x = %d, y = %d", x, y);
x = x ^ y;
y = x ^ y;
x = x ^ y;
printf("\nAfter Swapping: x = %d, y = %d", x, y);
return 0;
}
Output of the above code:
Before swapping: x = 40, y = 55
After Swapping: x = 55, y = 40
Python program to swap two numbers
In the given Python program, we ask the user to enter two integer values and store them in two variables. Then, we swap the entered value using the addition and subtraction operators.
x = int(input("Enter the value of x: "))
y = int(input("Enter the value of y: "))
# Displaying values
print("Before swapping: %d %d\n" %(x,y))
# swapping numbers
x = x + y
y = x - y
x = x - y
# Verifing swapped values
print("\nAfter swapping: %d %d\n"%(x,y))
Output of the above code:
Enter the value of x: 300
Enter the value of y: 160
Before swapping: 300 160
After swapping: 160 300
PHP program to swap two numbers
<?php
echo "Please enter the value of x: ";
$x = readline();
echo "Please enter the value of y: ";
$y = readline();
echo "Before swapping numbers: ";
echo $x." ".$y."\n";
/* Swapping numbers*/
$x = $x + $y;
$y = $x - $y;
$x = $x - $y;
echo "After swapping numbers: ";
echo $x." ".$y;
?>
Output of the above code:
Please enter the value of x: 150
Please enter the value of y: 100
Before swapping numbers: 150 100
After swapping numbers: 100 150
Related Articles
Java program to find area of triangleArea of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to convert Decimal to Octal
C program to convert decimal to binary
C program to check whether a number is even or odd