C++ Armstrong NumberC++ Average of N NumbersC++ Biggest of Three NumbersC++ Common DivisorsC++ Composite NumberC++ Condense a NumberC++ Even NumberC++ Factorial of a NumberC++ Fibonacci SeriesC++ GCDC++ LCMC++ Leap YearC++ Multiplication TableC++ Number CombinationC++ Odd NumberC++ Palindrome NumberC++ Perfect NumberC++ Prime FactorC++ Prime NumberC++ Sum of DigitsC++ Swapping Two Numbers

C++ Array ProgramsC++ Converter ProgramsC++ Star Patterns ProgramC++ Number Patterns ProgramC++ Alphabet Patterns ProgramLast Updated:

In the following example, we will find the average of numbers between 1 and 10 using for loop.

C++ Compiler

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int start = 1;
int end = 10;
int total = 0;
int count = (end - start) + 1;
float average = 0;
cout << "Average of numbers between " << start << " and " << end << ":\n";
for(start=start; start<=end; start++)
total += start;
cout << "\nTotal = " << total;
cout << "\nCount = " << count;
average = (float) total / count;
cout << "\nAverage is " << average;
return 0;
}

In the following example, we will find the average of numbers between 1 and 10 using while loop.

C++ Compiler

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int start = 1;
int end = 10;
int total = 0;
int count = (end - start) + 1;
float average = 0;
cout << "Average of numbers between " << start << " and " << end << ":\n";
while(start <= end)
{
total += start;
start++;
}
cout << "\nTotal = " << total;
cout << "\nCount = " << count;
average = (float) total / count;
cout << "\nAverage is " << average;
return 0;
}

In the following example, we will find the average of numbers between 1 and 10 using do while loop.

C++ Compiler

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int start = 1;
int end = 10;
int total = 0;
int count = (end - start) + 1;
float average = 0;
cout << "Average of numbers between " << start << " and " << end << ":\n";
do{
total += start;
start++;
}while(start <= end);
cout << "\nTotal = " << total;
cout << "\nCount = " << count;
average = (float) total / count;
cout << "\nAverage is " << average;
return 0;
}

In the following example, we will find the average of N numbers between the user given range.

C++ Compiler

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int start, end;
cout << "Enter a (int) starting number: ";
cin >> start;
cout << "Enter an (int) ending number: ";
cin >> end;
int total = 0;
int count = (end - start) + 1;
float average = 0;
cout << "Average of numbers between " << start << " and " << end << ":\n";
for(start=start; start<=end; start++)
total += start;
cout << "\nTotal = " << total;
cout << "\nCount = " << count;
average = (float) total / count;
cout << "\nAverage is " << average;
return 0;
}