C Programming

C Program to Swap Two Numbers

In C programming, swapping two numbers means switching the values of two variables. If there are two numbers in the C program, one is assigned to variable X, and the other is assigned to the variable Y. Suppose the value of X=20, Y=40, then after swapping the values will be X=40 and Y=20.

This blog will explore multiple ways to swap the two numbers in C Programming.

C Program to Swap Two Numbers

In C, there are three common ways to swap two numbers:

1: Swap Two Numbers Using a Temporary Variable

A temporary variable is the easiest way to swap two numbers in C. You only need to understand the simple concept of swapping.

In the below-mentioned program, we have used the third variable named temp to swap the values of two variables. First, we have assigned the value of the first number to the temporary variable, then the value of the first number is assigned to the second number and then the temp value is assigned to the second number.

#include <stdio.h>

int main()

{

    int first_number, second_number, temp;
    printf("Enter Value of First Number: ");
    scanf("%d", &first_number);
    printf("\nEnter Value of Second Number: ");
    scanf("%d", &second_number);
    temp = first_number;
    first_number = second_number;
    second_number = temp;
    printf("\nAfter Swapping: first number is: %d, second number is: %d", first_number, second_number);
    return 0;


}

2: Swap Two Numbers Without Using a Temporary Variable

In this method, there is no third variable is required for storing the values. You just have to perform simple addition and subtraction and store the values in variable 1 and variable 2. Below is a process of swapping the two numbers without using a temporary variable:

number1 = number1 + number2;

number2 = number1 - number2;

number1 = number2 - number1;

Let’s see the below simple example code for swapping the two numbers:

#include <stdio.h>

int main() {

  int x, y;
  printf("Enter the value of x: ");
  scanf("%d", &x);
  printf("Enter the value of y: ");
  scanf("%d", &y);
  //swap two numbers x and y
  x = x - y;  
  y = x + y;
  x = y - x;
  printf("After Swapping, x = %d\n", x);
  printf("After Swapping, y = %d\n", y);
  return 0;


}

3: Swap Two Numbers Using the Custom Function

In C, you can also create a function for swapping the values of two variables and call the function whenever you want to swap the values. You must use a pointer integer for swapping the two local variables.

In the below-given example code, the function swap takes two integer pointers as argument number1 and number2. The swap functions will exchange the values of numbers in the memory location by creating the temporary integer variable named temp. It first stores the value of the first_number to the temp, then the value of the first_ number is assigned to the second_number and then the temp value is assigned to the second number at the memory location.

#include <stdio.h>

void swap(int * number1, int * number2) {

int temp = * number1;

* number1 = * number2;

* number2 = temp;

}

  int temp = * number1;
  * number1 = * number2;
  * number2 = temp;
}
int main() {
  int first_number, second_number;
  printf("Enter Value of First number: ");
  scanf("%d", & first_number);
  printf("\nEnter Value of Second number: ");
  scanf("%d", & second_number);
  swap( & first_number, & second_number);
  printf("\nAfter Swapping: First number = %d, Second number = %d", first_number, second_number);
  return 0;


}

Conclusion

Swapping the values of two variables means, exchanging the values and changing the data in the memory. There are various ways to swap two numbers in C programming. We have discussed the methods including with temp variable, without temp variable, and using a function in the above section of the guide.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.