The Wayback Machine - https://web.archive.org/web/20240609213241/https://www.geeksforgeeks.org/c-program-swap-two-numbers/
Open In App

C Program to Swap two Numbers

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Swapping two numbers means exchanging their values. In this article, we will learn how to swap two numbers in the C programming language.

swap two numbers in c

Swap Two Numbers Using a Temporary Variable

Below is the C program to swap two numbers using a temporary variable.

C
// C program to swap two variables
#include <stdio.h>

// Driver code
int main()
{
    int x, y;
    printf("Enter Value of x ");
    scanf("%d", &x);
    printf("\nEnter Value of y ");
    scanf("%d", &y);

    // Using a temporary variable to swap the values
    // Store the value of x in a temporary variable
    int temp = x;
    // Assign the value of y to x
    x = y;
    // Assign the value stored in the temporary variable to
    // y
    y = temp;

    printf("\nAfter Swapping: x = %d, y = %d", x, y);
    return 0;
}

Output
Enter Value of x 
Enter Value of y 
After Swapping: x = 0, y = 0

Explanation

We swap two numbers by storing the value of one number in a temporary variable, assigning the value of the second number to the first number, and then assigning the value stored in the temporary variable to the second number.

Swap Two Numbers Without Using a Temporary Variable

Below is the C/C++ program to swap two numbers without using a temporary variable.

C
// C program to swap two numbers
// without using a third variable
#include <stdio.h>

// Driver code
int main()
{
    int a, b;
    printf("Enter the two numbers : \n");
    scanf("%d %d", &a, &b);
    printf("Before swapping A is : %d and B is %d \n", a,
           b);

    // Swapping without using a third variable
    // Sum of both numbers is stored in 'a'
    a = a + b;
    // Difference of sum and original 'b' is
    // stored in 'b'
    b = a - b;
    // Difference of sum and new 'b' is stored in
    // 'a'
    a = a - b;

    printf("After swapping A is : %d and B is : %d \n", a,
           b);
    return 0;
}

Output
Enter the two numbers : 
Before swapping A is : 0 and B is 32767 
After swapping A is : 32767 and B is : 0 

Complexity Analysis

Explanation

The idea is to use arithmetic operators. We will take the sum of the two numbers and store it in one number and store the difference of both numbers in the other number. Finally, we will store the difference of both numbers in the first number. Since the values will be updated after the first two operations we will be able to swap the numbers.

We can write a function also to swap twp variables if we need to repeatedly use swap

The idea is to use pointers in C. We use pointers because variables are passed by value in C and to change value of a variable inside another function, we need to pass address of the variable so that the value can changed by going to the address.

C
// C program for QuickSort
#include <stdio.h>

// function to swap tp integers
void swap(int *p1, int *p2) {
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

// Driver code
int main()
{
    int x, y;
    printf("Enter Value of x ");
    scanf("%d", &x);
    printf("\nEnter Value of y ");
    scanf("%d", &y);

    // Note that we are passing addresses
    // of two variables
    swap(&x, &y);
    
    printf("\nAfter Swapping: x = %d, y = %d", x, y);
    return 0;
}


Refer to the complete article on How to swap without using a temporary variable? for more details!



Similar Reads

Python Program to swap two numbers without using third variable
Given two variables n1 and n2. The task is to swap the values of both the variables without using third variable.Examples: X : 10 Y : 20 After swapping X and Y, we get : X : 20 Y : 10 A : 'Hello' B : 'World' After swapping A and B, we get : A : 'World' B : 'Hello' Method 1 :- Using simple built-in method left , right = right , left This method work
4 min read
C++ Program to Swap Two Numbers
Swapping numbers is the process of interchanging their values. In this article, we will learn algorithms and code to swap two numbers in the C++ programming language. 1. Swap Numbers Using a Temporary Variable We can swap the values of the given two numbers by using another variable to temporarily store the value as we swap the variables' data. The
4 min read
Javascript program to swap two numbers without using temporary variable
To swap two numbers without using a temporary variable, we have multiple approaches. In this article, we are going to learn how to swap two numbers without using a temporary variable. Below are the approaches used to swap two numbers without using a temporary variable: Table of Content Using Arithmetic OperatorsUsing Bitwise XORA mixture of bitwise
6 min read
TCS Coding Practice Question | Swap two Numbers
Given two numbers, the task is to swap the two numbers using Command Line Arguments. Examples: Input: n1 = 10, n2 = 20 Output: 20 10 Input: n1 = 100, n2 = 101 Output: 101 100 Approach: Since the numbers are entered as Command line Arguments, there is no need for a dedicated input lineExtract the input numbers from the command line argumentThis extr
3 min read
Swap Two Numbers using Function in C++
Swapping numbers means exchanging the values of the two numbers with each other. For Example, Before Swapping:a = 10, b = 22;After swapping:a = 22, b = 10In this article, we will write a program to swap two numbers using a function in C++. How to Swap Two Numbers Using Function in C++?We will pass the address or reference of the two variables that
3 min read
Swap Two Numbers Without Third Variable in C++
In C++, swapping two numbers means we need to exchange the value of two numbers. In this article, we will learn how to swap two numbers without using the third variable in C++. Example Input: a=10b=20Output:After swapping:a=20b=10Swap Two Numbers Without Using a Third VariableIn C++ we can swap two numbers without using a temporary variable by usin
2 min read
How to swap two numbers without using a temporary variable?
Given two variables, x, and y, swap two variables without using a third variable. Method 1 (Using Addition and subtraction) The idea is to get a sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from the sum. C/C++ Code // C++ Program to swap two numbers without // using temporary variable #include
21 min read
How to Swap Two Numbers Using Pointers in C++?
Swapping the values of two numbers is a very common operation in programming, which is often used to understand the basics of variables, pointers, and function calls. In this article, we will learn how to swap two numbers using pointers in C++. Example Input:int x=10;int y=20;Output:int x=20;int y=10;Swap Two Numbers Using Pointers in C++In C++, po
3 min read
C++ Program to swap two members using Friend Function
Pre-requisite: Friend FunctionCASE 1: Given two numbers a &amp; b, swap these two numbers using the friend function of C++. Examples: Input : a = 5, b = 9 Output : a = 9, b = 5 Input : a = 4, b = 6 Output : a= 6, b = 4 Approach: Create a class Swap, declare three variables in it, i.e., a, b, and temp and create a constructor for inputs. Declare a f
3 min read
Python Program to Swap Two Variables
Given two variables x and y, write a Python program to swap their values. Let's see different methods in Python to do this task. Method 1: Using Naive approachThe most naive approach is to store the value of one variable(say x) in a temporary variable, then assigning the variable x with the value of variable y. Finally, assign the variable y with t
3 min read