Program to swap two numbers without using third variable:
#include <stdio.h>
int main()
{
int number1=15, number2=25;
printf("Intially number1=%d number2=%d",number1,number2);
number1=number1+number2;//number1=40 (15+25)
number2=number1-number2;//number2=15 (40-25)
number1=number1-number2;//number1=25 (40-15)
printf("\nAfter swap number1=%d number2=%d",number1,number2);
return 0;
}
Output:
Intially number1=15 number2=25
After swap number1=25 number2=15
#include <stdio.h>
int main()
{
int number1=15, number2=25;
printf("Intially number1=%d number2=%d",number1,number2);
number1=number1+number2;//number1=40 (15+25)
number2=number1-number2;//number2=15 (40-25)
number1=number1-number2;//number1=25 (40-15)
printf("\nAfter swap number1=%d number2=%d",number1,number2);
return 0;
}
Output:
Intially number1=15 number2=25
After swap number1=25 number2=15
No comments:
Post a Comment