How to swap two numbers without using third variable in C Program?

How to swap two numbers without using third variable in C Program?

Senior Developer Asked on February 11, 2017 in C.
Add Comment
1 Answer(s)
#include<stdio.h>
 
int main() {
   int a, b;
 
   printf("\nEnter value for 1st number & 2nd number : ");
   scanf("%d %d", &a, &b);
 
   a = a + b;
   b = a - b;
   a = a - b;
 
   printf("\nAfter swapping value of a : %d", a);
   printf("\nAfter swapping value of b : %d", b);
 
   return (0);
}

 

Program Output

Enter value for 1st number & 2nd number :  30   40
After swapping value of a : 40
After swapping value of b : 30
Team Leader Answered on February 24, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.