Start learning free

LESSON 160 OF 347 · LOOPS - CHALLENGES

Print Powers in C#: A while Loop That Multiplies

Practice Read the full explanation

Given two non-negative integers A and B, print to the console all numbers less than or equal to B that are powers of A

Powers of a number are: A0, A1, A2, etc.

An = A multiplied by itself n times

A0 = 1
A1 = A
A2 = A * A
A3 = A * A * A
A4 = A * A * A * A
etc.

Example:

Input: A = 3, B = 100

Output:
1
3
9
27
81

Explanation:
30 = 1
31 = 3
32 = 9
33 = 27
34 = 81
Hints:

We start with the integer 1. We should repeatedly do some instructions (print it then multiply it by A) as long as one condition is satisfied (current power <= B). Sounds familiar?

We should use a variable power, initially equal to 1, in order to store the current power. And we should use a while loop to repeatedly check if power <= B and if so, print and update power. How will the code look like?

power = 1
while power <= B:
	print(power)
	power *= A


STEP 1 OF 4
    Ctrl + Enter to check

    C# · LOOPS - CHALLENGES

    Print Powers in C#, explained

    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

    1. Start at 1. Declare an int called power with the value 1, because the zeroth power of any number is 1.
    2. Write the loop condition. while (power <= B) keeps going as long as the current power is allowed. If B is 0, the condition is false immediately and nothing is printed.
    3. Print inside the loop. Console.WriteLine(power) prints the number followed by a line break.
    4. Move to the next power. power *= A multiplies by A. 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.WriteLine accepts an int directly. There is no need to convert it to a string first.
    • power *= A is shorthand for power = 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:

    Checkpowerpower <= 100Action
    11trueprint 1, then power becomes 3
    23trueprint 3, then power becomes 9
    39trueprint 9, then power becomes 27
    427trueprint 27, then power becomes 81
    581trueprint 81, then power becomes 243
    6243falsethe 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, so power never grows past B and the loop never ends (when B is at least 1). The problem allows A = 1 as a non-negative integer, so a robust version would handle it, for example by printing 1 once and returning.
    • A = 0. After printing 1, power becomes 0. Since 0 is at most B, it is printed forever. Zero needs the same kind of guard.
    • B = 0. The condition 1 <= 0 is false, so nothing is printed. That is correct: 1 is larger than 0, and no power of a positive A is 0.
    • B near the top of the int range. C# integer arithmetic wraps around silently by default. With A = 2 and B = int.MaxValue, the power after 1,073,741,824 would be 2,147,483,648, which doesn't fit in an int. It wraps to -2147483648, which passes the <= B test, and the loop goes on with nonsense values. Using long for power avoids this.
    • Using < instead of <=. The task says "less than or equal to", so a power equal to B must be printed. For A = 10 and B = 1000 the output ends with 1000.
    • Initializing power with A. That skips the zeroth power, 1.

    Testing

    • A = 3, B = 100 prints 1, 3, 9, 27, 81.
    • A = 10, B = 1000 prints 1, 10, 100, 1000.
    • A = 2, B = 1 prints 1 only.
    • A = 5, B = 0 prints nothing.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free