Power of a Number: flowchart, algorithm and program

This program reads a base x and a non-negative exponent n, then prints x raised to the power n. It does this without any library function: it starts with result = 1 and multiplies it by x exactly n times.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set result = 1.
  3. Step 3: Read x.
  4. Step 4: Read n.
  5. Step 5: Set i = 1.
  6. Step 6: If i <= n, go to step 7; otherwise go to step 9.
  7. Step 7: Set result = result * x.
  8. Step 8: Set i = i + 1. Go to step 6.
  9. Step 9: Print x, n, result.
  10. Step 10: Stop.

Pseudocode

BEGIN
    result ← 1
    READ x
    READ n
    i ← 1
    WHILE i <= n DO
        result ← result * x
        i ← i + 1
    END WHILE
    PRINT x, n, result
END

Program in C, Python and Java

Power of a Number program in C

#include <stdio.h>

int main() {
    double x, result = 1;
    int n, i;
    printf("Enter base x: ");
    scanf("%lf", &x);
    printf("Enter exponent n: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        result = result * x;
    }
    printf("%g^%d = %g\n", x, n, result);
    return 0;
}

Power of a Number program in Python

x = float(input("Enter base x: "))
n = int(input("Enter exponent n: "))
result = 1
for i in range(1, n + 1):
    result = result * x
print(f"{x:g}^{n} = {result:g}")

Power of a Number program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double x, result = 1;
        int n, i;
        System.out.print("Enter base x: ");
        x = sc.nextDouble();
        System.out.print("Enter exponent n: ");
        n = sc.nextInt();
        for (i = 1; i <= n; i++) {
            result = result * x;
        }
        System.out.println(x + "^" + n + " = " + result);
    }
}

Sample input: x = 2, n = 5
Output: 2^5 = 32

How it works

  • Read the base into x (a double, so decimals like 2.5 work) and the exponent into n (an int).
  • Start with result = 1. This is the right starting value because multiplying by 1 changes nothing, and it is also the answer when n = 0.
  • The loop runs for i = 1 to n, so the body executes exactly n times. Each time, result = result * x.
  • For x = 2 and n = 5, result goes 1 → 2 → 4 → 8 → 16 → 32, so the program prints 2^5 = 32.
  • After the loop ends, print x, n and result. The %g format drops unnecessary trailing zeros, so 32 prints as 32 and not 32.000000.

Dry run (trace table)

StepLineBoxresultxniOutput
13Start
24result = 11
37Read x12
49Read n125
510i = 11251
610i <= n?1251
711result = result * x2251
810i = i + 12252
910i <= n?2252
1011result = result * x4252
1110i = i + 14253
1210i <= n?4253
1311result = result * x8253
1410i = i + 18254
1510i <= n?8254
1611result = result * x16254
1710i = i + 116255
1810i <= n?16255
1911result = result * x32255
2010i = i + 132256

Time and space complexity

Time: O(n) · Space: O(1). The loop runs n times doing one multiplication each time, using only a few variables.

Common mistakes

  • Starting result at 0 instead of 1. Every product then stays 0, so the answer is always 0.
  • Looping from i = 0 to i <= n, or from i = 1 to i < n. Either runs the wrong number of times and gives x^(n+1) or x^(n-1).
  • Reading x with %d or n with %lf in scanf. The format specifier must match the variable type, or the values are garbage.

Viva questions

Q1. Why is result initialised to 1 and not 0?

Because we multiply into result each time. Starting at 0 would make every product 0. 1 is the multiplicative identity, so it also gives the right answer x^0 = 1 when the loop doesn't run.

Q2. Why is x declared as double while n is an int?

The base can be a decimal like 2.5, so it needs double, and result must be double too to hold fractional or large values. The exponent counts loop iterations, so it is a whole number and int fits.

Q3. What does the program print when n = 0?

The condition i <= n is false at the start (1 <= 0), so the loop body never runs and result stays 1. That is correct since any number to the power 0 is 1.

Q4. What happens if the user enters a negative exponent, like n = -2?

The loop does not run because 1 <= -2 is false, so the program prints 1, which is wrong (the answer should be 1/x^2). To fix it, compute with |n| and take the reciprocal when n is negative.

Q5. Why does scanf use %lf for x but %d for n, and why is %g used in printf?

%lf reads a double and %d reads an int; using the wrong specifier gives garbage. In printf, %g prints a double in a compact form, without trailing zeros, and switches to exponent notation for very large or small values.

Q6. What is the time complexity of this program, and how many multiplications does it do?

It does exactly n multiplications, so the time complexity is O(n) and the space is O(1).

Q7. What would change if you wanted a faster method for large n?

Use exponentiation by squaring: square the base and halve the exponent each step, multiplying into the result when the exponent is odd. This reduces the work from O(n) to O(log n) multiplications.

Q8. What happens if x = 0 and n = 0, or if the result gets very large?

With x = 0 and n = 0 the program prints 1, which matches the usual convention 0^0 = 1. If the result exceeds the range of double, it overflows to inf, which is printed as inf.

Frequently asked questions

What does the program print when n = 0?

It prints 1. The loop body never runs because i = 1 is already greater than n = 0, so result keeps its starting value of 1, which matches x^0 = 1.

Why use double for x and result instead of int?

A double lets the base be a decimal such as 1.5 and holds large results that would overflow an int. With int, even 2^31 would go wrong.

Can this program handle a negative exponent?

No. With n < 0 the loop never runs and it prints 1, which is wrong. To support it, compute with |n| and then take 1 / result, or use the pow() function from math.h.

More programs

← All programs

Try AI Flowchart free →