I’m trying to write a program that will take two numbers and depending on their values, return both are odd, both are even, or A is odd and B is even, etc.
I managed to get the program to check one variable but if I add in the second variable I get an output that I don’t expect and I can’t seem to be able to arrange the code to give the correct output. I’m guessing that it is a problem with the arrangement of the if/else statements.
#include <stdio.h>
int main()
{
int numA, numB;
printf("Please enter variables:");
scanf("%d, %d", &numA, &numB);
if (numA % 2) {
printf("Variable A:%d is odd \n",numA);
}
else{
printf("Variable A:%d is even \n", numA);
}
if (numB % 2) {
printf("Variable B:%d is odd \n",numB);
}
else{
printf("Variable B:%d is even \n", numB);
}
return 0;
}
The output I get is below
Please enter variables:4 5
Variable A:4 is even
Variable B:32767 is odd
Why is variable B:32767 not 5?
Many thanks for the help as ever. If I can sort this out, hopefully I can figure out the rest for myself.
Your
scanfformat expects the numbers to be separated by a comma,but your input wasn’t, so only the first number was converted by
scanfand the second was uninitialised.You should always check the return value of
scanfand friends to verify that the correct number of conversions was made.