Using a for loop how do u raise a number to the power of another number?

Im stuck on a problem can someone please help. Are instructor wants us to use the for loop method instead of a Pow function and wants us to go as high as the 5th power.
This is the assignment:
Write a program that will ask the user for two integer numbers.
Use a for loop to raise the first number to the power of the second number.
I recommend using the long type instead of int type.


This is what i have so far.. Can someone give me some advice and also tell me what im doing wrong..Thx


#include <iostream>

using namespace std;

int main()

{

long int i;

long int y;

long int total;

total = 1;

cout << "Enter a number: ";

cin >> i;

cout << "Raise first number to the power of: ";

cin >> y;

for(i=1; i<=y; i++){


total = i * i *i;

}



cout << i << '\t' << "to the" << '\t' << y << '\t' << "power is: " << total << '\n';



system("pause");

return(0);

}
1
2
3
for(i=1; i<=y; i++){
     total = i * i *i;
}


If you do it that way the number of times you multiply it by itself will be constant.

what you want to do is multiply the first number by itself, and loop through that the exact number of times as your second number.

Hint: you'll need to do something like.

1
2
total = i;                  // place this somewhere in your code
total = total * i;       // place this somewhere else in your code 


I'm sure you can figure out the rest. Good Luck.
What does the following code do?

1
2
3
for(i=1; i<=y; i++){
total = i * i *i;
}


The first time through the loop i == 1, so total is set to the value of 1 * 1 * 1 (1).
The second time through the loop i == 2, so total is set to the value of 2 * 2 * 2 (8).
The third time... and so forth, until the last time through the loop where i == y, so
total is set to the value of y * y * y (ie, y^3).

Well i was missing with it right now and this is what i did...It gives me the 5th power of any number but if i ask it to give me the 3rd power of a number it sill gives me the 5th..Is there something i need to change that i might be doing wrong..



using namespace std;

int main()

{
long int i;

long int x;

long int y;

long int total;

total = 1;

cout << "Enter a number: ";

cin >> x;

cout << "Raise first number to the power of: ";

cin >> y;

for(i=1; i<=y; i++){


total = x*x*x*x*x;

}



cout << x << '\t' << "to the" << '\t' << y << '\t' << "power is: " << total << '\n';



system("pause");

return(0);

}
It gives you the 5th power because you multiply x by itself 5 times.

Think.

You need to multiply x by itself y times.
Out of the graciousness of my heart I'll give you another hint.

The following code prints out the sum of the first N integers.

1
2
3
4
5
int sum = 0;
for( int i = 1; i <= N; ++i )
    sum += i;

std::cout << "The sum is " << sum << std::endl;


Is there something i need to change that i might be doing wrong..


 
total = x*x*x*x*x;


you need to let the loop determine how many times it will multiply the number by itself, and not hard code it into the equation.
You could use the 'pow' function and stick the power you want in as the argument somewhere inside your for loop.
1
2
for(i=1; i<=y; i++){
}


Hey guys, I'm pretty new at this as well.

Just wondering, but aside from the inside of the loop, isn't there a problem with the loop instructions? For example, if the user inputed a number to the power of 1, wouldn't the loop still run one time and output a number to the power of two?

The confusing part to me is why use 5 as a limit? Wouldn't it be the same thing no matter what?

Would it be worth it to try and build a loop based program that deals with negative exponents and fractions as well for learning purposes?
Last edited on
It can still be done with the loop as it is written, however a slight change has to be
made to the code.
Here's how I would've done it up to the end of his loop with what I changed on line 14:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace std;
int main()
{
long int i;
long int x;
long int y;
long int total;
total = 1;
cout << "Enter a number: ";
cin >> x;
cout << "Raise first number to the power of: ";
cin >> y;
for(i=1; i<=y; i++){
total = total*x;
}


Guess I spoiled it for him... I got confused because if I were working on this from scratch I probably would have set total = x and set the loop condition to i<y. Same thing just cutting out one loop iteration? Although I guess the way it is now has the added advantage of calculating stuff to the power of 0.

I still don't get why you would design it to go up to the power of 5.
Last edited on
Topic archived. No new replies allowed.