I am stuck on a homework problem. What I am suppose to do is write a program that calculates the square of all the intergers from 1 till the number a user enters. So my program should calculate something like 1^2 + 2^2 + ...... n^2. I know I am suppose to use the for loop or the while loop, but I am lost. This is what I came up with so far:
using while loop
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x;
cout << "Please enter a number: ";
cin >> x;
int i = 1;
while (i < x)
{
x++;
int sum=i+x;
cout << "Result: " << pow(sum,2) << endl;
}
return 0;
}
using for loop
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x;
cout << "Please enter a number: ";
cin >> x;
for (int i = 1; i< x+1; x++)
cout << "The sum of the square of all intergers from
1 to the number you have entered is: "
<< pow(x=i+x,2) << endl;
return 0;
}
Any help and guidance would be greatly appreciated.