# Thread: Average of odd and even

1. ## Average of odd and even

Code:
```#include <stdio.h>

int main()
{
int num,x;

int evenCount = 0;
int oddCount = 0;
int evenSum = 0;
int oddSum = 0;
float evenAvg = 0;
float oddAvg = 0;

while (num !=0)
{
printf("Enter a number (0 to stop): ");
scanf("%d", &num);

if (x == 0)

return 0;

else
{
if ( num % 2 != 0)
{
oddCount++;
oddSum = num + oddSum;
}
else if( num % 2 == 0)
{
evenCount++;
evenSum = evenSum + num;
}
}
}

printf("\n");

evenAvg = evenSum / evenCount;
oddAvg = oddSum / oddCount;

printf("Odd Average %.1f\n", oddAvg);
printf("Odd Count: %d\n", oddCount);

printf("\n");

printf("Even Average %.1f\n", evenAvg);
printf("Even Count: %d\n",evenCount - 1);

printf("\n");

return 0;
}```
I'm new to C and trying to learn.

My problem is that the average is wrong and when I don't put any odd numbers it crashes. how do I fix this?

average is supposed to be the sum of number divided by the how many numbers there are 2. Remember that you initialized the values with 0. Hence there can be a case of divide by 0. 3. When you divide two integers, the result is an integer. You should cast at least one of them into a float if you want the result to be a float.
No, the compiler can't read your mind to do it on its own. 4. If your using the %lf format type, it's generally better to use
type double for your decimal numerical values. Also you declare
num but do not initialize it with zero. Your loop test will only be true
if num is zero, so it's a good idea to declare num as such, to avoid
possible logical errors down the line. 5. If your using the %lf format type, it's generally better to use
type double for your decimal numerical values.
I agree, though actually, he seems to be formatting %f to .1 precision. but even so %Lf is for long doubles. The lowercase l should only really be used with long integers, c for wchar_t arguments, or s for wchar_t strings. Popular pages Recent additions average, evensum, int, num, printfn; 