# An interactive C program that reads in integers until a zero is entered and display total of odd and even numbers and average of odd and even numbers

By | August 7, 2013

Let’s identify variables needed for this program.
First variable will be the one which will save the value entered by the user and it will be NUM. Other variables will be according to the output requirements that are Total of even numbers (TOTAL_EVEN), Total of odd numbers (TOTAL_ODD), Average of even numbers (AVG_EVEN), Average of odd numbers (AVG_ODD) and To Calculate Average we need the Total Count of even numbers(EVEN) and Total Count of odd numbers (ODD) in all seven variables.

The identified variables are NUM, TOTAL_EVEN, TOTAL_ODD, AVG_EVEN, AVG_ODD, EVEN, ODD.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

In this program there is a requirement of read a value until a zero is entered. It indicates LOOP is to be taken. Here we have FOR loop, WHILE loop and DO-WHILE loop. For this condition DO-WHILE loop is the best suited because the program has to scan the value at least once before terminating the program and then check for a value to be entered by the user is zero or not. If yes than it has to proceed again and it continues until the condition becomes false and stops.
Loop selected is DO-WHILE loop.

Now the variables may have garbage value so initialize them to zero. Give the user a message to enter a value and scan the input from the user and save it in NUM variable. To identify whether the given number is even or odd we have to divide that number by two. If the remainder is zero than number entered is an even number and if the remainder is one than number entered is a odd number. This can be achieved by % (modulus) operator which is giving us the remainder.

Since the remainder will always be either zero or one. We will go for SWITCH-CASE construct. The condition will be NUM%2 and cases will be zero and one (i.e. two cases). We will add the number received in either TOTAL_EVEN or TOTAL_ODD depending on the case zero or case one.

To calculate average we need the total count of the even numbers and the total count of the odd numbers entered. So after adding NUM to TOTAL_EVEN or TOTAL_ODD depending on the case zero or case one we will count the even numbers and odd numbers by adding one to EVEN variable and ODD variable respectively.

Whenever we are using division so divide by zero condition may arise anytime. So if user is entering zero in the beginning of the program there will be divide by zero condition i.e. exception or error. To avoid this we will use if condition checking EVEN and ODD not equal to zero in the program. This solves the problem, print the output values at the end of this program.

program code:

 Source code
```#include
void main()
{
int NUM,TOTAL_EVEN,TOTAL_ODD,AVG_EVEN,AVG_ODD,EVEN,ODD;
TOTAL_EVEN=TOTAL_ODD=AVG_EVEN=AVG_ODD=EVEN=ODD=0;
clrscr();
do
{
printf("\nENTER ANY NUMBER TO CONTINUE AND ZERO TO STOP : ");
scanf("%d",&NUM);
switch(NUM%2)
{
case 0: TOTAL_EVEN=TOTAL_EVEN+NUM;
EVEN=EVEN+1;
break;
case 1: TOTAL_ODD=TOTAL_ODD+NUM;
ODD=ODD+1;
break;
}
}while(NUM!=0);
if(EVEN!=0 && ODD!=0)
{
AVG_EVEN=TOTAL_EVEN/EVEN;
AVG_ODD=TOTAL_ODD/ODD;
}
printf("\nTOTAL OF EVEN NUMBER IS : %d",TOTAL_EVEN);
printf("\nTOTAL OF ODD NUMBER IS : %d",TOTAL_ODD);
printf("\nAVERAGE OF EVEN NUMBER IS : %d",AVG_EVEN);
printf("\nAVERAGE OF ODD NUMBER IS : %d",AVG_ODD);
getch();
}```

screen shots:-