View All C Programs

 Previews Example  View All C Programs Next Example 

Program to Swap Two Numbers in C

This program asks user to enter two numbers and this program will swap the value of these two numbers.
#include <stdio.h>
#include <conio.h>
      int a, b, temp;
      printf("Enter value of a: ");
      printf("Enter value of b: ");
      temp = a;    /* Value of a is stored in variable temp */
      a = b;       /* Value of b is stored in variable a */
      b = temp;    /* Value of temp(which contains initial value of a) is stored in variable b*/
      printf("\nAfter swapping, value of a = %d", a);
      printf("After swapping, value of b = %d", b);
Enter value of a: 10

Enter value of b: 20

After swapping, value of a = 20

After swapping, value of b = 10