Swapping of Two Numbers in C without using Temporary variables:
First, see the: Swapping of two numbers in C
First, see the: Swapping of two numbers in C
Swapping of Two number program in C without using Temporary variables:
#include<stdio.h>
int main()
{
int a;
int b;
printf("Type value of A = ");
scanf("%i",&a);
printf("\nType value of B = ");
scanf("%i",&b);
a = a + b;
b = a - b;
a = a - b ;
printf("A = %i",a);
printf("\nB = %i",b);
return 0;
}
int main()
{
int a;
int b;
printf("Type value of A = ");
scanf("%i",&a);
printf("\nType value of B = ");
scanf("%i",&b);
a = a + b;
b = a - b;
a = a - b ;
printf("A = %i",a);
printf("\nB = %i",b);
return 0;
}
Output:
Type value of A = 5
Type value of B = 2
A = 2
B = 5
Explanation:
- Two integers a, b is declared of type int.
- The values of a, b are taken from the keyboard given by the user.
- Now the value of both the values is added and stored in "a". Now the new "a" value the "b" value subtracted and the value is stored in "b". i.e ( 7 - 2 = 5) .
- Now "b" value is"5" and again we subtracting "a - b" where "b" value is "5". And the result of "a - b" i.e( 7 - 5 = 2) is stored in "a".
- And last Printf Prints the value of “a” and “b”.
See also: