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.
#include <iostream>
#include <cstdlib>
#include <cmath>
usingnamespace 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<<"."<<endl;
}
else
{
cout<<"Press enter to quit."<<endl;
}
return 0;
}
cout <<"The sum of the squares from 1 to "<< number<< " is "<< sum<<"."<<endl;
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