Write a C++ program to find the average of 5 numbers.
#include<iostream> using namespace std; // main function int main() { // variables to store input values double Number1, Number2, Number3, Number4, Number5; double Average; // to store result // input values by the user cout << "Enter five numbers: "; cin >> Number1 >> Number2 >> Number3 >> Number4 >> Number5; // calculating the average value Average = (Number1 + Number2 + Number3 + Number4 + Number5)/5; // show result cout << "Average = " << Average << endl; return 0; }
Output
Enter five numbers: 4
5
6
7
8
Average = 6
Write a C++ code to find the average of 5 numbers using for loop and array.
#include<iostream> using namespace std; // main function int main() { // Array to store 5 numbers double T4Tutorials[5]; double sum; // to store sum double avg; // to store result // take input values cout << "Enter five numbers: "; for (int loop = 0; loop < 5; ++loop ) { cin >> T4Tutorials[loop ]; } // calculate sum value sum = 0; for (int loop = 0; loop < 5; ++loop ) { sum += T4Tutorials[loop ]; } // calculate average value avg = sum/5; // display result cout << "Sum = " << sum << endl; cout << "Average = " << avg << endl; return 0; }
Output
Enter five numbers: 9
8
7
66
5
Sum = 95
Average = 19