Home > Article > Backend Development > 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
12 + 22 + 32 = 1 + 4 + 9 = 14 14/3 = 4.666667
#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!