Average of Squares of n Natural Numbers?

CServer Side ProgrammingProgramming

Given a number n, we need to find the average of the square of natural Numbers till n. For this we will first The squares of all the numbers till n. then we will add all these squares and divide them by the number n.

Input 3
Output 4.666667

Explanation

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

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;
}
raja
Published on 19-Aug-2019 12:40:15
Advertisements