Write a program that reads integer from the keyboard and count the numbers of integers and calculates the sum and the maximum.
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include<conio.h>
void main()
{
int n; /* number read from the keyboard */
int nbr = 0; /* counter */
int sum = 0; /* Sum */
int max; /* Maximum */
int scan; /* Number of values read by scanf */
/* Read a first integer to initialize max */
printf("Enter an integer : ");
scan = scanf("%d", &n);
max = n;
/* reading more inetegrs */
while (scan > 0)
{
/* updating the values */
nbr++;
sum += n;
if (n > max)
max = n;
/* reading a new integer */
printf("Enter an integer : ");
scan = scanf("%d", &n);
}
printf("Counter = %d, sum = %d, maximum = %d\n", nbr, sum, max);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Isomorphic binary trees