Contact Learn C
Copy Program Share Program

Program 13:Swapping two values without using 3rd variable

Program 13:
 
#include<stdio.h>
main()
{
  int a,b;
  printf("Enter a\n");
  scanf("%d",&a);
  printf("Enter b\n");
  scanf("%d",&b);
  printf("The values of a is %d and b is %d before swaping\n",a,b);
  a=a+b;
  b=a-b;
  a=a-b;

  printf("The values of a is %d and b is %d after swaping\n",a,b);

}
Explanation:
  1. Here we initialized a,b,c
    • a---> for storing 1st number
    • b--->for storing second number
  2. Now here goes the logic(let us take a=1 and b=3)
    • a=a+b, Therefore, a=1+3=4
    • b=a-b, Therefore b=4-3=1
    • a=a-b, Therefore a=4-1=3
    • Now the values of a and b are 3 and 1
  3. The values of a and are printed.
Output:
Swapping two values without using 3rd variable

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide