CODE OF GEEKS


A PLATFORM TO LEARN | CODE | ACHIEVE


" Programming is the real test of your creativity,its all about how you turn ideas into codes. "



Connect :


THIS WEBSITE IS BEST VIEWED IN THE GOOGLE CHROME WEB BROWSER.



TO SWAP TWO NUMBERS WITHOUT USING THIRD VARIABLE IN C

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

Process 1 : num1=num1-num2 num1=20-10 => 10 Process 2 : num2=num1+num2 num2=10+10 => 20 Process 3 : num1=num2-num1; num1=20-10 => 10 Now num1=10 & num2=20 ,hence numbers are now swapped.


CODE OF GEEKS

A Platform to LEARN | CODE | ACHIEVE

Connect :



INTERESTING READ FOR YOU :

SHARE:



For any Queries,suggestions,feel free to Mail us at :
codeofgeeks@gmail.com

For reporting any kind of copyright voilation,mail us at : cogcopyclaim@gmail.com

© copyright CODE OF GEEKS 2018