Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main(int argc, char * argv[]) {
- float accumulator = 0.0;
- int num_of_times = 0;
- int ii = 1;
- float average = 0.0;
- // Ask until we have a valid input
- while (1) {
- // Get the user input
- printf("How many?: ");
- scanf("%d", &num_of_times);
- printf("\n");
- // If number of times to run is less than 0,
- // tell them, and try again
- if (num_of_times <= 0) {
- printf("Please enter a number greater than 0.\n\n");
- } else{
- // We have a valid number now, break out of this loop
- break;
- }
- }
- // Now that we have the number of times to ask
- // the user for a number, do it
- for (ii = 0; ii < num_of_times; ii++) {
- float user_input = 0.0;
- // Get the user input to add to
- // the accumulator
- printf("User Number: ");
- scanf("%f", &user_input);
- // Add the input
- accumulator += user_input;
- }
- // Get the average
- average = accumulator / (float)num_of_times;
- // Display it to the user
- printf("\nAverage = %f\n", average);
- return 0;
- }
Add Comment
Please, Sign In to add comment