0
[C++] Program that reads a positive integer maximum limit.
Hi! Can anyone help me figure out this, please? I need a program that reads a positive integer maximum limit, a positive integer base, and displays all the powers of the base, less than the specified limit maximum value. I really have no idea how to even start that. :(
4 odpowiedzi
+ 9
Assuming you don't understand the task at hand:
We need a max limit, a base, and all powers of the base which results in values less than the max limit. E.g.
Limit = 100
Base = 2
Powers:
2^0 = 1
2^1 = 2
2^2 = 4
...
2^6 = 64
Base 2 to the power of 7 is 128, which is larger than the limit, so we stop at 2^6.
What you have to do, is receive input and store them into variables max and base respectively. Utilize a loop to start printing the powers of the base, while the resulting value is smaller than the limit.
If you can come up with your own attempt, even a bit of pseudocode, we can continue to focus on making it work.
+ 5
Lutor27 Nice try! I would suggest something like
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int max, base;
int power = 0, tmp = 1;
cout<<"Enter a maximum positive limit\n";
cin>>max;
cout<<"Enter a positive base\n";
cin>>base;
while(tmp<max)
{
cout << base << "^" << power << " = " << tmp << endl;
tmp = pow(base,++power);
}
}
+ 1
#include <iostream>
using namespace std;
int main()
{
int max;
int base;
int power;
cout<<"Enter a maximum positive limit\n";
cin>>max;
cout<<"Enter a positive base\n";
cin>>base;
power=base;
while(power<max)
{
for(int n=1; n<power; n++)
{
power*=base;
}
}
cout<<base<<" to the "<<power<<endl;
}
is this ok??
+ 1
Thank you so much!! I was pretty close though lol. I need to keep improving my coding (mostly logical thinking and making good algorithms).