C Program to Swap Two Numbers

In this example, We are going to swap two numbers. For interchanging two number we use a third variable for temporally storing value.

Program to swap two numbers in c

#include<stdio.h>
void main()
{
  int a,b,temp;
  printf("Enter any number : ");
  scanf("%d",&a);
  printf("Enter any other number : ");
  scanf("%d",&b);
  printf(" ** Before swapping ** \n");
  printf("a = %d \n",a);
  printf("b = %d \n",b);
  temp = a;
  a = b;
  b = temp;
  printf(" ** After swapping ** \n");
  printf("a = %d \n",a);
  printf("b = %d \n",b);
}
  

Output

Enter any number : 10
Enter any other number : 20
 ** Before swapping **
a = 10
b = 20
 ** After swapping **
a = 20
b = 10
Share in a Single Click

Leave a Reply

Your email address will not be published. Required fields are marked *