Swap two numbers using C

Swap number means interchanging the value of two variables. if the value of variable a = 10 and b = 20 then after swapping the value of variable a will be 20 and b will be 10. In this article we will swap numbers using various way.

Swap numbers using a third variable

In this program we will swap two number using a third variable called temp. We will use this temp variable to swap number as follows.

// swap number using a temp variable
#include <stdio.h>
int main(){
  int a, b, temp;

  printf("Enter first integer : ");
  scanf("%d", &a);
  printf("Enter second integer : ");
  scanf("%d", &b);

  temp = a;
  a = b;
  b = temp;

  printf("\nAfter Swapping the numbers are\n\n");
  printf("First integer is = %d\n", a);
  printf("Second integer is = %d\n", b);

  return 0;
}

Output of the swap numbers program

swap numbers in C, swap two number

Swap numbers without a temp variable

In this program we will will not use any temp variable to swap two numbers. Program for swapping of two numbers using C without a third variable is given bellow.

// swapping two numbers without a temp variable
#include <stdio.h>
int main(){
   int firstNumber, secondNumber;

   printf("Enter first number : ");
   scanf("%d", &firstNumber);
   printf("Enter second number : ");
   scanf("%d", &secondNumber);

   firstNumber = firstNumber + secondNumber;
   secondNumber = firstNumber - secondNumber;
   firstNumber = firstNumber - secondNumber;

   printf("\nAfter swapping\n\n");
   printf("First number = %d\n", firstNumber);
   printf("Second number = %d\n", secondNumber);
  return 0;
}

See output of program

swap numbers program in C, swap number using temp

Swap number using bitwise operator

// swap numbers using bitwise operator in C
#include <stdio.h>
int main(){
  int i, j;

  printf("Enter first number here : ");
  scanf("%d", &i);
  printf("Enter second number here : ");
  scanf("%d", &j);


  printf("\nBefore swapping i = %d and j = %d\n", i, j);

  i = i ^ j;
  j = i ^ j;
  i = i ^ j;

  printf("\nAfter swapping i = %d and j = %d\n", i, j);

 return 0;
}

Output of program:

Enter first number here : 34
Enter second number here : 67

Before swapping i = 34 and j = 67

After swapping i = 67 and j = 34

Swap two numbers using pointer in C

// swap numbers using pointer in C
#include <stdio.h>
int main(){
   int temp, m, n, *x, *y;

   printf("Enter number 1 : ");
   scanf("%d", &m);
   printf("Enter number 2 : ");
   scanf("%d", &n);

   printf("\nBefore Swapping number 1 = %d and number 2 = %d\n\n", m, n);

   x = &m;
   y = &n;

   temp = *y;
   *y   = *x;
   *x   = temp;

   printf("\nAfter Swapping number 1 = %d and number 2 = %d\n\n", m, n);

   return 0;
}

Output of swap number program:

Enter number 1 : 54
Enter number 2 : 89

Before Swapping number 1 = 54 and number 2 = 89


After Swapping number 1 = 89 and number 2 = 54


Process returned 0 (0x0)   execution time : 7.346 s
Press any key to continue.

Swap two numbers using function in C

// swap numbers using fuction in C
#include <stdio.h>

void swapNumbers(int *x, int *y){     //Swap function
   int temp;

   temp  = *y;
   *y = *x;
   *x = temp;
}

int main(){
   int a, b;

   printf("Enter the value of a : ");
   scanf("%d",&a);
   printf("Enter the value of b : ");
   scanf("%d",&b);

   printf("\nBefore Swapping value of a = %d and b = %d\n\n", a, b);

   swapNumbers(&a, &b);

   printf("\nAfter Swapping value of a = %d and b = %d\n\n", a, b);

   return 0;
}

program output:

swapping of two number using function, swap using pointer

Previous pagePalindrome numbers

Next pageReverse number