idk why my program is not working, i wanted to create a program that counts even and odd number when a user inputs number except 0. Zero is the terminator of the loop.

#include <stdio.h>

int main(){

    int entNum, ans;
    int even;
    int odd;

    do{
        even=0;
        odd=0;
        scanf("%d",&entNum);
        ans = entNum%2;

    if(ans==0){
        even++;
    }

    else if(ans!=0) {
        odd++;

    }   
    }while(entNum!=0);

    printf("Even: %d",even);
    printf("Odd: %d",odd);

    getchar();
}

Initialize even and odd to 0 when you declare them. Don't reset them inside of your loop.

In your example "0" is terminator but "0" allways incrases "even".
Check if(entNum==0){ break; } after scanf.
More convenient is binary compare to 1 for test even and odd in my opinion e.g. lines 15-22 you can replace to one line: entNum & 1 ? odd++ : even++ ; variable "ans" is unnecessary.

if you want to search very easy coding for number is even or odd in vb then you must read this, here i have provide the very easy source code for number is even or odd. by using this code you can display and also count even and odd number separately.

commented: 3 years late. That could be taken as spam. +0

PLUS his example is in vb, not C/C++.

PLUS his example is the equivalent of "count all the hooves and divide by four".

PLUS he doesn't believe in comments, white space or indentation in his code.

By all accounts, it is over-designed and just all around a bad example of how to write code.

commented: So it is as good as free beer. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.