0

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.

2
  • 1
    printf("odd %d \n", &oddCount); should be printf("odd %d \n", oddCount); - same for all the other printf calls Commented Feb 23, 2022 at 20:47
  • There are to many ifs. 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 (assuming 0 belongs to one of these categories. If not, it is just another if ). And these checks should be completely decoupled because there is no relation between these two properties.
    – Eugene Sh.
    Commented Feb 23, 2022 at 20:48

1 Answer 1

1

For starters you need to change this call

scanf("%c", &x);

to

scanf(" %c", &x);

Pay attention to the space before the conversion specifier.

And instead of trying to output addresses

printf("odd %d \n", &oddCount);
printf("even %d \n", &evenCount);
printf("positive %d \n", &posCount);
printf("negative %d \n", &negCount);

you have to write

printf("odd %d \n", oddCount);
printf("even %d \n", evenCount);
printf("positive %d \n", posCount);
printf("negative %d \n", negCount);

And it is better to substitute the while loop

while(x!='y') {
 //... 
 printf("if you want to end the loop write 'y'\n");
 scanf("%c", &x);
}

for do-while loop

do {
   //...
   x = '\0';
 printf("if you want to end the loop write 'y'\n");
 scanf(" %c", &x);
} while ( x != 'y' && x != 'Y' );

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.