C program for swapping of two Variables

Logic:
           we have two variables of type int say a and b. we want to shift the value of a into b and vice versa.
For this purpose we need another temporary variable say temp. we will shift value of a into temp. Then value of b into a and at last value of temp into b.

Code:

#include<stdio.h>
#include<conio.h>

void main()
{
   int a=20, b=30, temp;
 
  printf("value of a is %d and b is %d\n",a,b);

  printf("After Swapping \n");

  temp=a;   //swapping
  a=b;        //swapping
  b=temp;  //swapping

  printf("value of a is %d and b is %d\n",a,b);

getch(); //halts screen

}

Post a Comment

CodeNirvana
Newer Posts Older Posts
© Copyright Encyclopedia of C | Designed By Code Nirvana
Back To Top