Write a program to swap two numbers without using third variable

// Write a program to swap two numbers without using third variable
// Eg: 2 3 -> 3 2, -4 6 -> 6 -4
#include
int main(){
    int a,b,c;
    printf("Enter first number:");              // input from user
    scanf("%d",&a);
    printf("Enter second number:");
    scanf("%d",&b);
    printf("Before swapping:%d %d\n",a,b);
    
    b = b+a;
    a = b-a;                                    // swapping using two variables
    b = b-a;
    printf("After swapping:%d %d\n",a,b);
    getch();
}

No comments:

Post a Comment