Exchange Value of two variables with the help of Third Variable In C :



#include <stdio.h>

int main() {
    int a, b, c;

    // Ask user to enter two numbers
    printf("Enter Value of a & b: ");
    scanf("%d%d", &a,&b);


    // Add the two numbers
    c = a ;
    a = b ;
    b = c ;

    // Display the result
    printf("value after Swap of :
            a=%d and b=%d",a,b);

    return 0;
}


Explanation

Prompting the User: The program prints the message Enter Value of a & b: to ask the user to input two integer values.

Reading Input: The scanf function reads two integer values entered by the user and stores them in the variables a and b.

Swapping Values:

The value of a is assigned to the temporary variable c.

The value of b is assigned to a.

The value stored in c (which was originally the value of a) is assigned to b.

Displaying Result: The program prints the swapped values of a and b.

Example Run

If the user inputs 5 for a and 10 for b, the output will be as follows:

output :

Enter Value of a & b: 5 10
value after Swap of     :
              
a=10 and b=5       


Explanation:
  • Initially, a = 5 and b = 10.
  • After swapping, a = 10 and b = 5.
  • The program then prints the swapped values.