### Sum of the squares

Hey I am trying to write a program that will take the sum of the squares and I can't figure out what is wrong with my code. Mathematically, it makes sense but the code makes the number way to large. Unless, I am just tried and can't see its driving me nuts. Here is my code.

 ``123456789101112131415161718192021222324252627`` ``````#include #include #include using namespace std; int main() { int number,sum; cout << "Enter a number greater than 0 (less than 1 to quit:) "; cin >> number; if (number > 0) { for(int counter = 1 ; counter <= number; counter++) { sum =sum + pow(counter,2); } cout <<"The sum of the squares from 1 to "<< number<< " is "<< sum<<"."<

On line 12 number is given a value from the user input.

Hint: On which line is sum given an initial value?
 cout <<"The sum of the squares from 1 to "<< number<< " is "<< sum<<"."<

So if I enter 10, sum should equal 14 (assuming 1 is a square number). The code you have would return 1 + 4 + 9 + 16 + ... + 81, the sum of the squares from numbers less than what I entered
I see I never gave sum an initial value. Thanks it works now. I just gave sum =0 lol.
Topic archived. No new replies allowed.