[Solved] C Programming with loops for exam


Your program should be

#include <stdio.h>

/* Readability is important. So, indent your code */
/* Notice how I corrected your code and also made it more readable */

int main(void){

    int even = 0;
    int odd = 0;
    int x = 0; /* Initialize x */
    /* int num; Unused variable */

    while(x <= 20 || x % 2 == 0) /* Stop when x is greater than 20 or x is odd */
    {
        printf("\nPlease enter a number");
        scanf("%d", &x);

        if (x % 2 == 0)
            even = even + 1; /* +1, not +x. Or `even++;` */
        else
            odd = odd + 1;   /* +1, not +x. Or `odd++;`  */
    }

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

    return 0;
}

14

solved C Programming with loops for exam