Calculate the sum, the product and the average of a sequence of non-zero digits entered on the keyboard, knowing that the sequence is ended by zero. Just count the numbers (0, 1 ... 9) when entering data and beep if the data comes out of this range.
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
void maiCOUNT()
{
int X;
int COUNT=0;
int SUM=0;
long PROD=1;
do
{
printf("Enter digit number %d: ",(COUNT+1));
scanf("%d", &X);
if (X<0||X>9)
printf("\a");
else if (X)
{
COUNT++;
SUM+=X;
PROD*=X;
}
else if (!X && N>0)
{ /* ony if 1 digit was accepted */
printf("Sum = %d \n", SUM);
printf("Product = %ld\n", PROD);
printf("Average = %f \n", (float)SOM/N);
}
}while (X);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
KM conversion