Table of contents
1.
Introduction 
2.
Using temporary variable
2.1.
Implementation 
3.
Using arithmetic operators
3.1.
Implementation
4.
Frequently Asked Questions
4.1.
What do you mean by the datatype of a variable?
4.2.
Does c language support function overloading?
4.3.
Is A++ faster than A+1?
4.4.
What is the call by reference in functions?
4.5.
What is typedef in c?
5.
Conclusion
Last Updated: Jun 26, 2024
Easy

C Program to Swap Two Numbers

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

In this blog, we will learn about one of the most basic Programming problems in which we will learn to swap two variables. We will first start with the problem statement then learn about the various methods we can use to solve this problem. We will begin with the first method, in which we will be using a temporary variable, and then the. Let'smethod, in which we use mathematical operators to achieve the task. So, let's discuss both approaches one by one in detail.

Also see: C Static FunctionShort int in C Programming and Tribonacci Series

Using temporary variable

The first approach to this problem uses another variable to swap the two numbers. This third variable is known as the temporary variable. The basic idea behind this approach is to preserve one of the variables using the temporary variable while its value is updated with the second variable. Then we use this temp variable to update the value of the first variable into the second variable.

The above approach can be broken down into three steps-

  • Storing the first variable's value into a temporary variable.
  • Copy the value of the second variable into the first variable.
  • Update the value of the second variable from the temporary variable.

Since you have developed an understanding of the approach, let's look at implementing the above approach.

Implementation 

#include <stdio.h>
int main()
{
    int X, Y;
    printf("Enter X- ");
    scanf("%d", &X);
    printf("\nEnter Y- ");
    scanf("%d", &Y);
    int temp=X;
    X = Y;
    Y = temp;
    printf("\nAfter swapping both the numbers: X = %d, Y = %d", X,Y);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Enter X- 8

Enter Y- 9

After swapping both the numbers: X = 9, Y = 8,

You can also read about dynamic array in c  and,  Unary operator overloading in c++.

Using arithmetic operators

It's time to learn another method in which we won't be using the temp variables for swapping the two variables. Instead, we will be using arithmetic operations. The idea behind this approach is simple maths, which can be explained using the example given below. Let's say we have two variables- X and Y. with their values equal to 6 and 8, respectively. 

Now if we add Y to X, ->X=X+Y, So, X= 14 and Y= 8.

Now. If we assign the difference of X and Y to Y -> Y=X-Y, thus, Y = 14-8=6. 

If we again assign the difference of X and Y to X -> X=X-Y, thus, X=14-6=8.

Thus we can see the value of both the variables has been swapped.

I hope you understood this approach with the above example, so let's move on to implementing this approach.

Implementation

#include <stdio.h>
int main()
{
    int X, Y;
    printf("Enter X- ");
    scanf("%d", &X);
    printf("\nEnter Y- ");
    scanf("%d", &Y);
    X=X+Y;   
    Y=X-Y;    
    X=X-Y;
    printf("\nAfter swapping both the numbers: X = %d, Y = %d", X,Y);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Enter X- 8

Enter Y- 9

After swapping both the numbers: X = 9, Y = 8


You can practice by yourself with the help of online c compiler.

Frequently Asked Questions

What do you mean by the datatype of a variable?

The variable's data type defines the type of valid values for that variable and how they will be stored in the memory during the program's execution.

Does c language support function overloading?

No, the C language does not support function overloading. One of the reasons for this is that it is a loosely typed language. Various data types can implicitly convert themselves to some other, only adding to the confusion during the program's execution.

Is A++ faster than A+1?

Yes, because A++ will be a unary operation, whereas A+1 is a binary operation with more calculation overhead.

What is the call by reference in functions?

Call by reference in a function means that while calling the function, we pass the address to the memory location where the parameters were stored and not their copy.

What is typedef in c?

We use the typedef keyword when we need to define alias/synonyms for an existing type in the C language.

Conclusion

In this article, we have extensively discussed the various methods we can use to swap two numbers in C language. we started with a method that uses a temporary variable. We learned about another method in which we do this task only with the help of arithmetic operators.

Also read reverse a number.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available; look at the interview experiences and interview bundle for placement preparations.

 

Live masterclass