Swapping of Two Numbers - C program to Swap two numbers



 c program to Swap two numbers

Learn C - C tutorial - c program to Swap two numbers - C examples - C programs

C program to Swap two numbers

  • Swapping means Interchanging.
  • To swap we need to declare a third variable which is an temporary variable.
  • For example: you have taken two variables a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4.

Sample Code

#include <stdio.h>
int main()
{
    int a=10,b=12,temp;
    printf("\na =10 ");
    printf("\nb =12 ");
    temp=a;
    a=b;
    b=temp;
    printf("\na = %d\tb = %d \n", a, b);
}

Output

a =10 
b =12 
a = 12   b = 10 


View More Quick Examples


Related Searches to Swapping of Two Numbers - C program to Swap two numbers