C++ Program to find Sum and Average of N Natural Numbers

C++ Program to find Sum and Average of N Natural Numbers

How to calculate sum of n numbers in C++? To get sum of n numbers in C++ program here we can add n numbers using for loop and then get the average by dividing it.

Sum and Average of n Numbers using C++ :



#include <iostream>
using namespace std;

int main() 
{
    int number, average, sum = 0, value = 1;
    
    cout << "Please Enter the Range : ";
    cin >> number;

    while (value <= number)
    {
        sum = sum + value;
        value = value + 1;
    }
    
    average = sum / number;

    cout << "Sum of Natural Numbers from 1 to " << number << " : " << sum << endl;
    cout << "Average of Natural Numbers from 1 to " << number << " : " << average;
    
    return 0;
}


Output :