A PLATFORM TO LEARN | CODE | ACHIEVE
" Programming is the real test of your creativity,its all about how you turn ideas into codes. "
Connect :
Code
#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
printf("ENTER TWO NUMBERS \n");
scanf("%d %d",&num1;,&num2;);
printf("FIRST NUMBER = %d \n",num1);
printf("SECOND NUMBER = %d \n",num2);
if(num1>=num2)
{
num1=num1-num2;
num2=num1+num2;
num1=num2-num1;
}
else if(num1<num2)
{
num1=num2-num1;
num2=num1+num2;
num1=num1-num2;
}
printf("SO,AFTER SWAPPING :\n");
printf("FIRST NUMBER = %d \n",num1);
printf("SECOND NUMBER = %d \n",num2);
return 0;
}
Output
ENTER TWO NUMBERS 10 20
FIRST NUMBER = 10
SECOND NUMBER = 20
SO,AFTER SWAPPING :
FIRST NUMBER = 20
SECOND NUMBER = 10
What we did - Our Approach
In this problem,we are supposed to swap the values of two numbers without using a third variable.Swapping means interchanging of values among variables.
without using a third variable takes place in three processes :
Let us assume that num1=20 num2=10, hence , num1 is greater than num2
See this code in :
Also see :
Participate in our MONTHLY CHALLENGE,and get a chance to get listed on our site and win the TITLE of "MR.GEEK".
Connect :
INTERESTING READ FOR YOU :
SHARE:
For any Queries,suggestions,feel free to Mail us at :
[email protected]
For reporting any kind of copyright voilation,mail us at : [email protected]
© copyright CODE OF GEEKS 2018