C Programming Tutorial

Advertisements

Write C program to swap two numbers without using third variable



       #include<stdio.h>
       #include<conio.h>
       void main()
       {

              int a,b,temp;
              clrscr();
              
              printf("\nEnter value of A : "); 
              scanf("%d",&a);

              printf("\nEnter value of B : "); 
              scanf("%d",&b);

              printf("\n\nValues before swapping :");
              printf("\nA : %d",a);
              printf("\nB : %d",b);

              a  = a + b;
              b = a - b;
              a = a - b;
              
              printf("\n\nValues after  swapping :");
              printf("\nA : %d",a);
              printf("\nB : %d",b);

       }
       
   Output : 

              Enter value of A : 89
              Enter value of B : 52
              
              Values before swapping :
              A : 89
              B : 52

              Values after  swapping :
              A : 52
              B : 89




Advertisement