Loading...
C Program to Swap Two Numbers

C Program to Swap Two Numbers

In this example, we will learn to swap two numbers in C programming.

Remainder :

To understand this example, you must have the knowledge of the following C programming topics:


Method 1 : Swap Numbers Using Temporary Variable

#include <stdio.h>
int main(){
  int num1, num2, mediator;
  printf("Enter two number: ");
  scanf("%d %d", &num1, &num2);
  mediator = num1;
  num1 = num2;
  num2 = mediator;
  printf("After swapping, First Number is %d\n", num1);
  printf("After swapping, Second Number is %d", num2);
  return 0;
}

Output

Enter first number: 12
Enter second number: 23
After swapping, First Number is 23
After swapping, Second Number is 12

Working of the above program

  • First we declare three integer variables num1, num2 to store user input and swap their values, mediator to use as medium of swaping values.
  • int num1, num2, mediator;
  • Then we ask the user to enter two numbers and take the user input and store the values in num1 and num2 using scanf() function.
  • printf("Enter two number: ");
    scanf("%d %d", &num1, &num2);
  • Then we initialize mediator with the same value as num1. Now, mediator and num1 has same value.
  • mediator = num1;
  • Then we assign num1 with the same value as num2. Now, num1 has the same value as the intial value of num2.
  • num1 = num2;
  • Then we assing the value of num2 with the same value as mediator which is assigned as the same value as num1. Now, num2 has the same value as the initial value of num1.
  • num2 = mediator;
  • Finally, we print the values of num1 and num2.
  • printf("After swapping, First Number is %d\n", num1);
    printf("After swapping, Second Number is %d", num2);

Method 2 : Swap Numbers Without Using Temporary Variables

#include <stdio.h>

int main() {
int num1, num2;
printf("Enter two number: ");
scanf("%d %d", &num1, &num2);
num1 = num1 - num2;
num2 = num1 + num2;
num2 = num2 - num1;
printf("After swapping, First Number = %d\n", num1);
printf("After swapping, Second Number = %d", num2);
return 0;
}

Output

Enter two numbers : 12 34
After swapping, First Number = 34
After swapping, Second Number = 12

Working of the above program

  • First we declare three integer variables num1, num2 to store user input and swap their values, mediator to use as medium of swaping values.
  • int num1, num2;
  • Then we ask the user to enter two numbers and take the user input and store the values in num1 and num2 using scanf() function.
  • printf("Enter two number: ");
    scanf("%d %d", &num1, &num2);
  • Then we calculate and assign the difference of both numbers to num1.
  • num1 = num1 - num2;
  • Then we calculate and assign the sum of both numbers to num2. As num1 is the difference of num1 and num2 i.e. when we add the new value of num1 with the initial value of num2 we get the exact same value as the intial value of num1.
  • num2 = num1 + num2;
  • Then we calculate and assign the difference of both numbers to num1. As num1 is the difference of num2 and num1, new value of num2 is same as intial value of num1 i.e. when we subtract the new value of num1 from new value of num2 we get the exact same value as the intial value of num2.
  • num1 = num2 - num1;
  • Finally, we print the values of num1 and num2.
  • printf("After swapping, First Number = %d\n", num1);
    printf("After swapping, Second Number = %d", num2);

- Related Topics