0
Write a program to perform 1square +2square+3square............. +nsquare.?
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;
}
+ 1
#include <iostream>
using namespace std;
int main(void)
{
int n;
cin >> n;
cout << (n*(n+1)*(2*n+1)/6) << endl;
return 0;
}