# where is the error? exchange of variables

2

Sorry but I'm new to c and I have to solve this exercise

Write the code of the program that requests the value of two integers and exchange them later, showing both the initial value of each variable and its final value.

But when running it does not print the values, it asks me to enter 2 numbers so that after I print them and then I exchange them and print them but it does not print the values

``````#include <stdio.h>

int main() {

int a,b;
printf("ingresa el primer valor: \n");
scanf("%d", &a);
printf("ingresa el segundo valor: \n");
scanf("%d", &b);
printf("el primer valor es: \n", a );
printf("el segundo valor es: \n", b);
a=b;
b=a;
printf("el nuevo valor de a es: \n", a );
printf("el nuevo valor de b es: \n", b);

return 0;
}
``````

asked by lalo hernandez 09.02.2017 в 04:47
source

3

The error you have is that you make the printf () without the modifier "% d" which is used to print whole numbers.

Here's the solution:

``````#include <stdio.h>

int main() {

int a, b, aux;

printf("ingresa el primer valor: \n");
scanf("%d", &a);

printf("ingresa el segundo valor: \n");
scanf("%d", &b);

printf("el primer valor es: %d\n", a );
printf("el segundo valor es: %d\n", b);

aux = a;
a   = b;
b   = aux;

printf("el nuevo valor de a es: %d\n", a );
printf("el nuevo valor de b es: %d\n", b);

return 0;
}
``````

Result

``````ingresa el primer valor:
10
ingresa el segundo valor:
15
el primer valor es: 10
el segundo valor es: 15
el nuevo valor de a es: 15
el nuevo valor de b es: 10
``````