Problem overview
PrintPowers(int A, int B) prints every power of A that is less than or equal to B, one per line, starting with the zeroth power, which is always 1. It returns nothing; the output is what the lesson's tests look at.
For A = 3 and B = 100 the powers are 1, 3, 9, 27 and 81. The next one, 243, is larger than 100, so the printing stops there.
Approach
You don't need an exponent function. Each power is the previous one times A, so a single variable that starts at 1 and gets multiplied by A on every pass produces the whole sequence. The number of passes isn't known in advance, but the stopping rule is clear: stop when the current power exceeds B. That is a job for a while loop, which repeats as long as its condition holds.
Step by step
- Start at 1. Declare an
intcalledpowerwith the value 1, because the zeroth power of any number is 1. - Write the loop condition.
while (power <= B)keeps going as long as the current power is allowed. IfBis 0, the condition is false immediately and nothing is printed. - Print inside the loop.
Console.WriteLine(power)prints the number followed by a line break. - Move to the next power.
power *= Amultiplies byA. Without this line the loop would print 1 forever.
The solution in C#
class Solution {
public void PrintPowers(int A, int B) {
int power = 1;
while (power <= B) {
Console.WriteLine(power);
power *= A;
}
}
}
Some C# details:
Console.WriteLineaccepts anintdirectly. There is no need to convert it to a string first.power *= Ais shorthand forpower = power * A.- The order of print and multiply matters. Printing first means 1 gets printed before the multiplication. Reversing the two lines would skip 1 and print a power that may exceed
B.
Tracing the loop
Following the variables for A = 3 and B = 100 shows exactly when the loop ends:
| Check | power | power <= 100 | Action |
|---|---|---|---|
| 1 | 1 | true | print 1, then power becomes 3 |
| 2 | 3 | true | print 3, then power becomes 9 |
| 3 | 9 | true | print 9, then power becomes 27 |
| 4 | 27 | true | print 27, then power becomes 81 |
| 5 | 81 | true | print 81, then power becomes 243 |
| 6 | 243 | false | the loop ends |
The condition is checked before every pass, including the first one, and the value that fails the check (243) is never printed. The loop runs once per power of A up to B, so the number of passes grows with the logarithm of B, base A, which is very small even for a large B.
Why a while loop and not a for loop
A for loop fits when you know how many repetitions you want, such as "n times". Here you know only when to stop: when the value gets too big. A while loop states that condition directly. A for loop could do the same job, for example for (int power = 1; power <= B; power *= A), which packs the three parts into one line, but the while form keeps each part visible while you are learning.
Edge cases and common mistakes
A = 1. Every power of 1 is 1, sopowernever grows pastBand the loop never ends (whenBis at least 1). The problem allowsA = 1as a non-negative integer, so a robust version would handle it, for example by printing 1 once and returning.A = 0. After printing 1,powerbecomes 0. Since 0 is at mostB, it is printed forever. Zero needs the same kind of guard.B = 0. The condition1 <= 0is false, so nothing is printed. That is correct: 1 is larger than 0, and no power of a positiveAis 0.Bnear the top of theintrange. C# integer arithmetic wraps around silently by default. WithA = 2andB = int.MaxValue, the power after 1,073,741,824 would be 2,147,483,648, which doesn't fit in anint. It wraps to-2147483648, which passes the<= Btest, and the loop goes on with nonsense values. Usinglongforpoweravoids this.- Using
<instead of<=. The task says "less than or equal to", so a power equal toBmust be printed. ForA = 10andB = 1000the output ends with 1000. - Initializing
powerwithA. That skips the zeroth power, 1.
Testing
A = 3,B = 100prints 1, 3, 9, 27, 81.A = 10,B = 1000prints 1, 10, 100, 1000.A = 2,B = 1prints 1 only.A = 5,B = 0prints nothing.