Swap two numbers without third variable

05 min read

C program to swap two number without using a temporary third variable is given below.

include<stdio.h> 
int main()    
{    
int a=5, b=4;      
printf("Before swap a=%d b=%d",a,b);      
a=a+b;                          //a=9 (5+4)    
b=a-b;                           //b=1(5-4)    
a=a-b;                           //a=1 (5-4)    
printf("\nAfter swap a=%d b=%d",a,b);    
return 0;  
}   

 

Output:

Before swap a=5 b=4 After swap a=4 b=5



POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error