Swap two numbers without using third variable

Ram Pothuraju

 #include <stdio.h>


int main()

{

    int a, b;


    printf("Enter two integers separated by a space: ");

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


    // Swapping the values of a and b

    a = a + b;

    b = a - b;

    a = a - b;


    printf("After swapping, a = %d and b = %d", a, b);


    return 0;

}



In this program, we first take two integers as input from the user. Then, we swap the values of a and b without using a third variable by adding a and b and storing the result in a, then subtracting the value of b from a and storing the result in b, and finally subtracting the value of the original b from a and storing the result in a.
Tags

Post a Comment

0Comments

Post a Comment (0)