For Exercise 1, we'll practice getting input from the user, displaying text and values to the console, implementing loop and basic arithmetic operations.
Problem:
Get 10 numbers from user. Output the sum, average, minimum and maximum.
#include<stdio.h>#define SIZE 10intmain() {
int num;
float ave, sum =0;
int max =0, min =0, i;
for (i =0; i < SIZE; i++) {
printf("Input a number: ");
scanf("%d", &num);
sum = sum + num;
if (max < num || i ==0) {
max = num;
}
if (min > num || i ==0) {
min = num;
}
}
printf("The sum is %.0f.\n",sum);
ave = sum/10;
printf("The average is %.2f.",ave);
printf("\nThe largest value is %d.", max);
printf("\nThe smallest value is %d.", min);
return0;
}
No comments:
Post a Comment