
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to swap two numbers without using the third or a temporary variable using C Programming?
With the help of addition and subtraction operations, we can swap two numbers from one memory location to another memory location.
Algorithm
The algorithm is explained below −
START
Step 1: Declare 2 variables x and y. Step 2: Read two numbers from keyboard. Step 3: Swap numbers. //Apply addition and subtraction operations to swap the numbers. i. x=x+y ii. y=x-y iii. x=x-y Step 4: Print x and y values.
Program
Following is the C program which explains swapping of two numbers without using third variable or a temporary variable −
#include<stdio.h> int main(){ int x,y; printf("enter x and y values:"); scanf("%d%d",&x,&y);// lets take x as 20 and y as 30 x=x+y;// x=20+30=50 y=x-y;//y=50-30=20 x=x-y;//x=50-20=30 printf("After swap x=%d and y=%d",x,y); return 0; }
Output
You will get the following output −
enter x and y values:20 30 After swap x=30 and y=20
Note − We can swap two numbers by using multiplication and division and bitwise XOR operators without taking third variable help.
Consider another example which explains how to swap two numbers by using multiplication and division operators.
Program
Following is the C program to demonstrate the respective functioning of swapping two numbers −
#include<stdio.h> int main(){ int x,y; printf("enter x and y values:"); scanf("%d%d",&x,&y); x=x*y; y=x/y; x=x/y; printf("After swap x=%d and y=%d",x,y); return 0; }
Output
When you execute the above program, you will get the following output −
enter x and y values:120 250 After swap x=250 and y=120
- Related Articles
- How to swap two arrays without using temporary variable in C language?
- Swap Numbers without using temporary variable in Swift Program?
- Write a Golang program to swap two numbers without using a third variable
- How to swap two numbers without using a temp variable in C#
- How can I swap two strings without using a third variable in Java?
- Swap two Strings without using third user defined variable in Java
- How to swap two String variables without third variable.
- Swap two Strings without using temp variable in C#
- Swapping two variable value without using third variable in C/C++
- How do I add two numbers without using ++ or + or any other arithmetic operator in C/C++?
- Java program to swap two numbers using XOR operator
- Find HCF of two numbers without using recursion or Euclidean algorithm in C++
- How to find the size of a variable without using sizeof in C#?
- How to Swap Two Variables using Python?
- Finding LCM of more than two (or array) numbers without using GCD in C++

Advertisements