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

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

WBOY
Release: 2023-09-15 08:53:02
forward
1380 people have browsed it

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
Copy after login

Explanation

is:

Explanation

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

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;
}
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template