Ok so I have this assignment that I have to do and this is what it says...
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.
Display the answer to the screen.
Here is the example that is given...
Example session (your messages may be different if you wish):
ITSE 1307 lab #4, John Q. Public
Enter a number: 5
Enter the power to raise 5 to: 3
5 to the 3 power is 125
What I dont understand is how or why you would use a for loop for this.
Can someone explain to me how you would use a for loop in this and maybe give me an example?
Provided you have learned arrays, you could store the integers in an array with 2 indices and use the for loop to iterate through them while getting user input.
I know the basics of an array but I guess i just don't understand a for loop well enough or something because I cannot see the connection between an array, a for loop, and user input.
I don't want to bother you but if you could, I would really appreciate a simple example.
Actually, even a simple example would give you the answer to your homework assignment, and we here, frown on doing that. With that said, here are clues.
Ask for the number. Make duplicate of it. Like 'answer=number;
Ask for a power number to raise number by.
Hopefully, you know how to create a for loop. for( int x=1;x<power#;x++)
Then have answer=answer*number;
The first time through is actually the number to the power of 2, second time, power of three, etc.
Ok thank you for the reply... I managed to do the project but I feel like I went about it all wrong and that even though i achieved what I wanted, I feel as though I did it completely wrong.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
double a,b,c;
long answer;
char do_again;
do {
for (do_again = 'y'; do_again == 'y';)
{
cout << "Please enter a number: " ;
cin >> a;
cout << "\n";
cout << "Enter the number you want to raise " << a << " to: " ;
cin >> b;
cout << "\n";
answer = pow(a, b);
cout << a << " to the power of " << b << " equals: " << answer << "\n\n";
cout << "Would you like to try again? (y for yes n for no): ";
cin >> do_again;
cout << "\n";
while (do_again != 'y' && do_again != 'n') {
cout << "You did not input y or n. Please try again: ";
cin >> do_again;
}
if (do_again == 'n') {
cout << "Ok thank you for participating. See you again!" << "\n";
}
}
}
while ( do_again == 'y');
system ("pause");
return 0;
}
Like my for loop just seems completely wrong and made up and i just want to be able to understand for loops better because I think I am not grasping them or something.
Also, I was advised to use long type for the numbers but when I declare a,b, and c as long types the program does not work. Any help there?
First, you do not need the for (do_again = 'y'; do_again == 'y';). That doesn't really do anything. Take that out plus the left and right braces '{ }'. Next, you had said in the first post, you were to find the power with a loop. Using the pow() function isn't it. Substitute the pow() function for the 'for loop' I talked about previously, and you'll have a good program.
You may want to change the long answer to double answer so as to not convert double to long, losing possible data.
Lastly, you'll probably get a few negative comments on your use of system ("pause");. It's REALLY frowned upon, since it's system dependent. It's probably better just to use cin >> a; after printing a statement like cout << "Press enter to end program . . ."; so the user knows to press a key and that the program isn't frozen up or something.
#include <iostream>
usingnamespace std;
int main()
{
double a,b;
double answer;
char do_again;
do {
cout << "Please enter a number: " ;
cin >> a;
cout << "\n";
answer = a;
cout << "Enter the number you want to raise " << a << " to: " ;
cin >> b;
cout << "\n";
for (int x = 1; x <= b; x++){
answer= answer*a;
}
cout << a << " to the power of " << b << " equals: " << answer << "\n\n";
cout << "Would you like to try again? (y for yes n for no): ";
cin >> do_again;
cout << "\n";
while (do_again != 'y' && do_again != 'n') {
cout << "You did not input y or n. Please try again: ";
cin >> do_again;
}
if (do_again == 'n') {
cout << "Ok thank you for participating. See you again!" << "\n";
}
}
while ( do_again == 'y');
system ("pause");
return 0;
}
Thank you for your help. The assignment has been finished. But i do have one question. It is not part of the assignment so you wouldn't be giving me any answer but i just want this program to be complete with no bugs so how would I go about calculating the answer if the base was raised to the power of 0?
Thanks,
Americo
Actually, Americo, your for (int x=1;x<=b;x++) is wrong. It should be only <, not <=. You can test it by inputting the numbers you first mentioned, 5 to the power of 3. It will come out as 625. With just the <, it will be the correct 125. And, I believe, a number to the power of 0, is equal to the number inputted. You can test that out also by typing those numbers into your program. And if I am proven wrong, that the number to the power of 0 is really a 0, then modify your program so that the inputted number must be 1 or higher, using a do/while loop.You know, Do while a is <=0, so the user must type a number larger than 0. You may want to do that anyway, to prevent the user from inputting negative numbers. Hope this helps.
oh hahah simple mistake. I meant to take that out and forgot. And actually no both of your answers are incorrect. A number to the power of zero is always 1 so how would I go about that? I tried using an if statement but it didn't work. Maybe I just put it in the wrong place or something. Or maybe a do/while loop might work. I don't know, just a thought.
And actually, adding negative numbers into this program would be a great idea so that it encompasses a full range of numbers from negative infinity to infinity, assuming I can work the zero power in hahah.