Sum of powers of numbers 1 through 10

I need help writing a program that will calculate the sum of any power of the numbers 1 through 10. So for example: 1^2 + 2^2 + 3^2.... + 10^2. Except that the power does not have to be 2, it can be any number that I enter.

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;

int Power (int exp){

int i, p, sum=0;

for(i=1; 1<=10; i++)
    for(p=0; p<=i || p>i; p++)
        {sum==pow(i, p)
        return sum;}


}

int main()
{
     cout << Power(5) << endl;
    return 0;
}


Right now it should give me the sum of the powers of 5, but its not working. I have a feeling its completely off. Any help?

Please explain the "not working".

Your code formatting is not intuitive.
Function "Power" computes a sum.
Statement on line 12 does not end with a semi-colon.
What is the purpose of loop on line 11?
Function Power returns the first time the execution reaches line 13, i.e. on the first iteration of the loops.
Line 12 has an equality comparison rather than accumulation.

One of the problems with your code is for(i=1; 1<=10; i++) which never stops and will cause overflow.

also
1
2
{sum==pow(i, p)
        return sum;}

will not compute all the values, just first one.

also you need to use double not int since std::pow returns double

1
2
3
4
5
6
7
8
9
double Power(double exp){

	double sum = 0.0;

	for (short i = 1; i <= 10; ++i)
		sum += pow(i, exp);

	return sum;
}
Last edited on
What is the purpose of loop on line 11?

i is the base, and p is the exponent. The first loop gives the numbers 1-10 for i. The second (and im not too sure I did it right) is supposed to give any number as p (the exponent).

Here is my revised code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;

double Power (double exp){

int i, p, sum=0;

for(i=1; 1<=10; i++)
    for(p=0; p<=i || p>i; p++)
        {sum=pow(i, p);}

        return sum;


}

int main()
{
     cout << Power(5) << endl;
    return 0;
}


Now it is giving me a blank output.

@codekiddy
for (short i = 1; i <= 10; ++i)


Whats with the "short"? Never seen that before.


Also pow(i, p) should give me i^p, correct?
Last edited on
When is 1 <= 10 false? When is p <=i || p > i false? I get that p is the power, but isn't your power exp? If you're given the exponent, you don't need to provide your own.

sum = pow(i, p) assigns i^p to sum, it doesn't add it.

short is a short, or 2-byte, integer. It can be useful for decreasing space needed by the program. Although, a char is 1 byte, so @codekiddy could have chosen a char to reduce space even more.

What your code does is set sum to every exponent of 1-10 (which is infinite), so it's not giving you no output, it's running infinitely.
When is 1 <= 10 false?


Sorry, that 1 is supposed to be an i. I'll fix that...

I get that p is the power, but isn't your power exp?


If I replace "exp" with "p" I get an error message saying, "declaration of int p shadows a parameter".

sum = pow(i, p) assigns i^p to sum, it doesn't add it.


Will fix that.

What your code does is set sum to every exponent of 1-10 (which is infinite), so it's not giving you no output, it's running infinitely.


So how do I fix this?

My intention is the following: Make the first loop give the numbers 1-10. The second loop should give me the powers (exponents). This can be ANY number, hence the condition p <=i || p > i.

That I use pow(i, p) and add it to sum which is intiialized to 0.

What am I doing wrong?


Ok, had some syntax errors, here is the newly revised code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;

double Power (double exp){

int i, p, sum=0;

for(i=1; 1<=10; i++)
    for(p=0; p<=i || p>i; p++)
        {sum=pow(i, p);}

        return sum;


}

int main()
{
     cout << Power(5) << endl;
    return 0;
}
Hi, I'm not very expert so be careful with the things that I write...
Anyway inside your function I will do something like that:

1
2
3
4
double sum=0;
for(int i=0; i<=10; i++)
sum+=pow(i,exp);
return sum;


Then inside the main:
1
2
3
4
double exponent;
cout<<"enter the exponent"<<endl;
cin>>exponent;
cout<<Power(exponent)<<endl;


I hope that you find it helpful.
Last edited on
This can be ANY number

Any one number, not all possible numbers simultaneously.

In your example main() you have chose the "any" to be 5. Just 5, not 0, 1, 2, 3, 4, 5, 6, 7, ... and all the other numbers up to infinity and beyond.


It should also be quite clear that you should not be using variable names line "i", because they can so easily hide in typo.
@Gi Pa
Thank you for the solution but for future reference, we try not to give answers here. It's easier for people to learn if you help them find the problem and figure out their own solution. If you just give them the solution, they won't know what was wrong in the first place, so they can't fix it in the future.

@Arslan7041
The question asks for you to give the sum of all numbers 1-10 to the exponent n. n can be any number, so you know it's input by the user or some other function (that's why your function call has exp).

Your most recent code still has 1 <= 10.

The line for(p = 0; p <=i || p > i; p++) causes an infinite loop (p <= i || p > i is never false; and is unnecessary. You receive your exponent in the function (exp), so you just use that (remove p altogether).

You were setting sum, not adding to it, so sum would always equal the most recent power, not the sum of all powers. Fix it by sum += pow(i, exp); or sum = sum + pow(i, exp). As has been pointed out, pow returns a double, so you should have a double for sum (you can find workarounds, but there's no good reason for you to do that (the function returns a double, and exp is a double).
Last edited on
@GRex2595
Thank you so much. It worked! As you stated, the p variable was unnecessary. This whole time I thought I needed a nested loops, when all I needed was a single loop.

Here is my final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;

int Power (int exp){

int i;
int sum=0;

for(i=1; i<=10; i++)
    sum+=pow(i, exp);

        return sum;


}

int main()
{
     cout << Power(5) << endl;
    return 0;
}



Notice, it works with either double or int for sum. So what did you mean when you said, "you should have a double for sum"?

Thank you to everyone who helped me in this thread.
You should have double for sum because if you accept a double for the exponent, then your pow will return a double. If you only accept integers, then it doesn't matter.
Topic archived. No new replies allowed.