0

Write a program to perform 1square +2square+3square............. +nsquare.?

10th Sep 2016, 7:10 AM
mukul joshi
2 Answers
+ 2
http://code.sololearn.com/cH2DxX2XTB9L #include <iostream> using namespace std; unsigned int sumOfSquares(unsigned int n) { int i, sum; sum = 0; for (i = 1; i <= n; i++) { sum += i*i; } return sum; } int main() { unsigned int n; cout << "Sum of squares (1 + 4 + 9 + ... + n^2)" << endl; cout << "Enter n: "; cin >> n; cout << n << endl; cout << sumOfSquares(n) << endl; return 0; }
10th Sep 2016, 8:08 AM
Zen
Zen - avatar
+ 1
#include <iostream> using namespace std; int main(void) { int n; cin >> n; cout << (n*(n+1)*(2*n+1)/6) << endl; return 0; }
12th Sep 2016, 9:11 AM
kiwiyou
kiwiyou - avatar