Home  >  Article  >  Backend Development  >  What is the average of the squares of n natural numbers?

What is the average of the squares of n natural numbers?

WBOY
WBOYforward
2023-09-15 08:53:021334browse

What is the average of the squares of n natural numbers?

Given a number n, we need to find the average of the squares of natural numbers up to n. To do this, we first find the square of all numbers less than or equal to n. Then we add up all these squares and divide them by n. The Chinese translation of

Input 3
Output 4.666667

Explanation

is:

Explanation

12 + 22 + 32 = 1 + 4 + 9 = 14
14/3 = 4.666667

Example

The Chinese translation is:

Example

#include<iostream>
using namespace std;
int main() {
   long n , i, sum=0 ,d;
   n=3;
   for(i=1;i<=n;++i) {
      d=i*i;
      sum+=d;
   }
   cout<<sum/n;
   return 0;
}

The above is the detailed content of What is the average of the squares of n natural numbers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete