In: Computer Science
Design, code, and test a program that accepts positive integers until the value 204 is entered. After input terminates, the program should report the total number of even integers (excluding the special number 204) , the average value of the even integers, the total number of odd integers, and the average value of the odd integers. The program should also report "Evens won!", "Odds won!", or "Evens and odds tied!" based on which total was greater. Test your program thoroughly and submit output with at least twelve (12) integers as input.
Please write in a C program.
C code:
#include <stdio.h>
int main()
{
//initializing even and odd number sum, even and
odd number count
float
even=0,odd=0,even_count=0,odd_count=0,num;
//accepting number
scanf("%f",&num);
//looping till 204 is entered
while((int)num!=204){
//checking if the number
is even
if((int)num%2==0){
//adding it to even
even+=num;
//incrementing even_count
even_count++;
}
else{
//adding it to odd
odd+=num;
//incrementing odd_count
odd_count++;
}
//accepting number
scanf("%f",&num);
}
//printing Total number of even integers
printf("Total number of even integers:
%d\n",(int)even_count);
//printing Average value of even integers
printf("Average value of even integers:
%.2f\n",even/even_count);
//printing Total number of odd integers
printf("Total number of odd integers:
%d\n",(int)odd_count);
//printing Average value of odd integers
printf("Average value of odd integers:
%.2f\n",odd/odd_count);
//checking if Average of even values is
greater
if(even/even_count>odd/odd_count)
//printing Evens won
printf("Evens
won!");
//checking if Average of odd values is
greater
else if(even/even_count<odd/odd_count)
//printing Odds won
printf("Odds
won!");
else
//printing Evens and odds tied
printf("Evens and odds
tied!");
return 0;
}
Screenshot:
Input and Output: