Write a program that declares two integer variables X and Y. The user must enter their values and then exchange their values (swap X and Y) X takes the value of Y, and Y takes the value of X.


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>

int main()
{
    int X , Y;
    int aux; 
    
    printf("Please enter 2 integers: ");
    scanf("%d %d", &X, &Y);
    printf("Values before the exchange: X = %d, Y = %d\n", X, Y);
    
    // exchange here
    aux = Y; 
    Y = X; 
    X = aux;
    
    // the following is also correct
    /*
    aux= X;
    X=Y;
    Y=aux;*/
    
    printf("Values after the exchange: X = %d, Y = %d\n", X, Y);
    
    return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Finding a value in an array using Interpolation search