Swap values of 2 variables without using 3rd variable

Swap values of 2 variables without using 3rd variable


Program:

#include<stdio.h>
void main()
{
int m,n;
printf(“Enter numbers: ”);
scanf(“%d%d”,&m,&n);                       //Suppose values of m and n are: m=5 n=3 //
printf("Values of numbers before swapping is: %d and %d\n”,m,n);
m=m+n;                                  //m=8 n=3//
n=m-n;                                   //m=8 n=5//
m=m-n;                                 //m=3 n=5 Thus, we get desired output//
printf("Values of numbers after swapping is: %d and %d\n”,m,n);
}

Output:






No comments:

Post a Comment