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