URI Online Judge | 1154
Ages
Write an algorithm to read an undeterminated number of data, each containing an individual's age. The final data, which will not enter in the calculations, contains the value of a negative age. Calculate and print the average age of this group of individuals.
Input
The input contains an undetermined number of integers. The input will be stop when a negative value is read.
Output
The output contains a value corresponding to the average age of individuals.
The average should be printed with two digits after the decimal point.
Input Sample | Output Sample |
34 56 44 23 -2 | 39.25 |
Solution:
#include <stdio.h> int main() { int n,cnt=0; double avg,sum=0; while(1) { scanf("%d", &n); if(n<0) break; else { sum+=n; cnt++; } } avg=sum/cnt; printf("%.2lf\n",avg); return 0; } |
Post a Comment
0Comments