I want this program to determine if given integers are positive/negative & odd/even then increment the counters. But it doesn't work properly.
include <stdio.h>
int main() {
int evenCount=0, oddCount=0, posCount=0, negCount=0, zeroCount=0, m;
char x='x';
while(x!='y') {
printf("enter an integer\n");
scanf("%d", &m);
if((m>0)&&(m%2==0)){
posCount+=1;
evenCount+=1;
}
else if((m>0)&&(m%2!=0)){
posCount+=1;
oddCount+=1;
}
else if((m<0)&&(m%2==0)){
negCount+=1;
evenCount+=1;
}
else if((m<0)&&(m%2!=0)){
oddCount+=1;
negCount+=1;
}
else {
zeroCount+=1;
}
printf("if you want to end the loop write 'y'\n");
scanf("%c", &x);
}
printf("odd %d \n", &oddCount);
printf("even %d \n", &evenCount);
printf("positive %d \n", &posCount);
printf("negative %d \n", &negCount);
return(0);
}
When I run it and give some numbers counts are at millions.
enter an integer
123
if you want to end the loop write 'y'
enter an integer
2
if you want to end the loop write 'y'
enter an integer
5
if you want to end the loop write 'y'
enter an integer
y
if you want to end the loop write 'y'
odd 6487572
even 6487576
positive 6487568
negative 6487564
The second example.
enter an integer
12
if you want to end the loop write 'y'
enter an integer
y
if you want to end the loop write 'y'
odd 6487572
even 6487576
positive 6487568
negative 6487564
I'm new to coding and this is my first post on this site also english is not my main language. I'm sorry if made any mistakes.
printf("odd %d \n", &oddCount);
should beprintf("odd %d \n", oddCount);
- same for all the otherprintf
callsif
s. Only one if/else is needed to determine if the number is even/odd, and only one if/else is needed to determine negative/positive (assuming0
belongs to one of these categories. If not, it is just anotherif
). And these checks should be completely decoupled because there is no relation between these two properties.