Search Your Query

Breaking

Friday, April 12, 2019

Swap Two Number Without 3rd variable



Swap Two Number Without 3rd variable

Program Code : 

#include<stdio.h>
int main(){
int a,b;
printf("Enter the value of a and b\n");
scanf("%d%d",&a,&b);
printf("Before swap two number a=%d and b=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;


printf("After the swap two number a=%d and b=%d",a,b);
return 0;
}

Hence , here a=10 and b=30 , now a=a+b=10+30=40 , means 'a' assign the value 40
              now b=a-b=40-30=10 , b assign the value 10 and  a=a-b=40-10=30 

Input : 

Enter the value of a and b
10
30

Output : 

Before swap two number a=10 and b=30

After swap two number a=30 and b=10

No comments:

Post a Comment