Skip to main content

write aSwapping two values in C without using temp variable:-

Swapping two values in C without using temporary variable:- 

#include<stdio.h>
   main()
   {
    int a,b;
    printf("enter the value of a:");
    scanf("%d",&a);
    printf("enter the value of b:");
    scanf("%d",&b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("a=%d",a);
    printf("b=%d",b);
   }
for example we take value of a=3 and b=2.
at first "a=a+b"performed then the value of "a=3+2;a=5",now the value of a is 5.
after that "b= a-b"performed then the value of "b=5-2;b=3",now the value of b is 3.
at last "a=a-b" performed then the value of "a=5-3;a=2",now the value of a is 2
so after swapping  the value of a is  and value of b is 3
You can take input any value of a&b.
If u write this program correctly then you see this output

Comments