How to Calculate and Display the Average of the 5 Numbers in C++

How to calculate and display the average of the 5 numbers in C++?

I Have solved my own question. As I said I am avoiding global variables, labels or go-to statements, infinite loops, and break statements to exit loops. So, here is my answer:

Here is my code:

#include <iostream>
using namespace std;

int main() {
// Creating variables.
float v, w, x, y, z, average;
// Prompting the user to enter first number.
cout << "Please enter first number: ";

// Getting the input from the user.
cin >> v;
// Prompting the user to enter second number.
cout << "Please enter second number ";

// Getting the input from the users.
cin >> w;
// Prompting the user to enter third number.
cout << "Please enter third number ";

// Getting the input from the user.
cin >> x;
// Prompting the user to enter fourth number.
cout << "Please enter fourth number ";

// Getting the input from the user.
cin >> y;

// Prompting the user to enter fifth number.
cout << "Please enter fifth number ";

// Getting the input from the user.
cin >> z;
// Calculating the average of those user input five numbers.
average = (v + w + x + y + z) / 5;

// Displaying the total average of the five numbers entered.
cout << "The average of the five numbers is:" << average << endl;
return 0;
}

Finding the average of 5 numbers and N numbers using only While and do while loop in C

First, you're using nas your while condition variable, but also as the variable to scan the input. If I start your program by scanning 20, for example, your while loop will exit on the first interaction. Use your i variable instead and also increment it every time your loop executes.

do{
...
}while(i <= 5);

Second, if you want only numbers between 1 and 10, then you should write a condition for it. For example:

printf("enter the number %d:\n", i); //do not increment it here!
scanf("%d",&n); //assuming "n" as your variable to scan
if(n > 0 && n < 11){
add += n;
i++; //increment it here instead!
}

Third, initialize your variables in order to not get thrash values

float add = 0;
float avg = 0;
int i = 1;

Finally, assign your result (not mandatory, but since you're using it I'll keep it):

avg = add/5.0f

and display:

printf("%.1f", avg);

Calculating the average of user inputs in c

#include <stdio.h>

int main()
{
int i = 0;
float num[100], sum = 0.0, average;
float x = 0.0;

while(1) {

printf("%d. Enter number: ", i+1);
scanf("%f", &x);

if(x == -1)
break;

num[i] = x;
sum += num[i];
i++;

}

average = sum / i;

printf("\n Average = %.2f", average);

return 0;
}

There is no need for the array num[] if you don't want the data to be used later.
Hope this will help.!!

Program to calculate the average of unknown set of numbers in C

You are almost done. Just count the number of elements and divide the sum by count to get the average. Here type-casting is also required, otherwise average of 2 and 3 will give you 2 which is incorrect.

#include <stdio.h>

int main() {
int numberEntered;
int sum = 0;

printf("Program to calculate the average of a series of numbers\n\n");
printf("Please enter the first number. Enter 0 to stop: ");
scanf("%d", &numberEntered);

int count_number = 0;

while (numberEntered != 0)
{
sum = sum + numberEntered;
count_number++;
printf("Please enter another number. Enter 0 to stop: ");
scanf("%d", &numberEntered);
}
if(count_number>0)
{printf("AVG: %f",((float)sum)/count_number);}

}


Related Topics



Leave a reply



Submit