/*
This is the sixth lab of Lab EE107
The purpose of this lab is to understand how the program calculates
the average of n decimal numbers.
The number n is input from the keyboard in C
The purpose is to create the code and free of syntax and grammatical errors
Author: Ekwegbalum Unachukwu
Star ID:do2170dt
Date: Feb 20, 2024.
*/
#include <stdio.h>
int main() {
int n_EU;
printf("Enter the value of n:\n ");
scanf("%d", &n_EU);
// Check if n is greater than 0
if (n_EU <= 0) {
printf("Please enter a valid positive value for n.\n");
return 1; // Exit with an error code
}
//declare a variable using the double.
double sum_EU = 0.0;
double number_EU;
int i = 0;
printf(" number of inputs: %d\n", n_EU);
// Use a while loop to read n numbers
while (i < n_EU) {
scanf("%lf", &number_EU);
printf("%.3f, ", number_EU);//.3f is three decimals after the period
sum_EU += number_EU;
++i;
}
// Calculate and print the average
double average_EU = sum_EU / n_EU;
printf("Average of %d numbers: %.2lf\n", n_EU, average_EU);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: