Swapping Two Numbers without Using Third Variable

 Q. Write a  C program to swap two numbers without using third variable.

Ans:

#include <stdio.h>


int main() {

    int a, b;


    printf("Enter two numbers: ");

    scanf("%d %d", &a, &b);


    printf("Before swapping: a = %d, b = %d\n", a, b);


    a = a + b;

    b = a - b;

    a = a - b;


    printf("After swapping: a = %d, b = %d\n", a, b);


    return 0;

}



Comments