For Loop and square root

Ok this is what i have:



#include <iostream>
using namespace std;

long int a; //number
long int b; //power
long int c; //square

int main ()
{
cout << " Enter a number ";
cin >> a;

cout << " Enter a power to raise by ";
cin >> b;

cout << " The result is ";
cin >> for( c == (a * a) * b);



system ("pause");
return 0;
}
This is what I have to do:

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.
using the long type instead of int type.
Display the answer to the screen.

And it has to come out kinda like this:

Enter a number: 5
Enter the power to raise 5 to: 3
5 to the 3 power is 125


my problem is i dont know how to or where to put a for loop in this or if I'm even doing it right plz help!!!

I've also been told that this would work

for(a=1; a <= size; a++){
for(b=1;b <= size; j ++)
printf("*");
printf("\n");

but i dont know how to use that or where to put it?!
Last edited on
Your instructions tell you how to use the for loop: "Use a for loop to raise the first number to the power of the second number."

So, something like this:

1
2
3
4
5
6
7
8
9
10
11

for( long i = 0; i < b; i++ )
    {
    if( i == 0 )
        square = a;
    else
        square *= a;

    }    /*    for( i < b )    */



Also, make your variables longs as indicated in your instructions.
Last edited on
Buddy, maybe you can use this program:

......................................................................................................................................................

#include<iostream>
#include"conio.h"
#include "math.h" //used for the (pow) function
using namespace std;

int main( );
{ unsigned long int a; //number
long int b; //power
long int c; //exponent (answer)

cout<<"enter a number" <<endl;
cin>>a;
cout<<"enter the power to raise by" <<endl;
cin>>b;

c=pow(a,b); //computer equivalent of a b
cout<<"the answer is" <<c;
_getch( );
}

..............................................................................................................................................


Using "unsigned long int" gives you more space to store your number, so you can both enter and receive extremely large numbers (till about 14 billion!).

But please note that the program has been written for Microsoft Visual Studio 2010 Ultimate.
If you are using some other compiler, just make the required changes.


By the way, there is no need for a loop as you only need to perform a single operation.

Let me know if it works.

Cheers.
Last edited on
Hey MasterP,

That certainly is a nice solution; however, I think that hatsumi141 has to follow the instructor's instructions. If not, then by all means, use MasterProgrammer's solution.
well, kooth,
if hatsumi141 really needs to borrow from the original program, I think using a "for" loop on the variable a until it has been multiplied with itself b times should be sufficient.

To hatsumi141:
if you want me to show you how to do that, all you have to do is ask.
That is what it says in his original post. I thought it was his homework ...
Topic archived. No new replies allowed.