Check Power of 3 in Java

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • Which numbers are powers of three: 1, 3, 9, 27, 81, …
  • The divide-by-3 loop: peel factors of 3 until you stop; you should land on 1 only for pure powers.
  • Two Java programs: one number (sample 27) and a 1–20 list (1 3 9).
  • A live preview that shows each division step, plus usual edge cases.

Overview

Picture multiplying by 3 again and again: 1 → 3 → 9 → 27 → 81 → … Any value on that train is a power of 3. To test a number n, run backward: while n is divisible by 3, replace n with n / 3. If you end exactly at 1, it is a power of 3.

Integer-safe logic

Only integer math with % and /, so no floating-point precision issues.

Live preview

Try 27 and watch 27 → 9 → 3 → 1 step-by-step.

Range practice

From 1 to 20, valid values are 1, 3, 9.

Prerequisites

Integer division and the remainder operator % in Java.

  • while loops and if conditions.
  • Knowing 27 % 3 == 0 means 27 divides evenly by 3.

What is a power of 3?

A power of three is any whole number 3k where k ≥ 0, such as 1, 3, 9, 27, 81, …

Every factor must be 3 only. That is why dividing by 3 until you cannot continue reveals whether the number is a pure power of 3.

Why repeated division works

If n = 3k, one division by 3 gives 3k-1. Keep doing this and powers of three end at 1. If you get stuck above 1 with n % 3 != 0, then n is not a pure power of 3.

After the loop

Stopping when n % 3 != 0 leaves a core value. The core is exactly 1 for valid powers of 3.

Quick examples

27Yes
Peel 3s
27 → 9 → 3 → 1
15No
Peel 3s
15 → 5 (stuck; 5 is not 1)
1Yes
Because
30 = 1; no division needed.

Live preview

Mirrors Java logic: reject non-positive values, then divide by 3 while remainder is 0. Uses safe integer checks.

Try 9, 10, or 1.

Live result
Press “Run check” to see the steps.

Algorithm

Goal: return true iff positive n equals 3k for some integer k ≥ 0.

Reject n <= 0

Powers of three are positive integers in this problem.

Strip factors of 3

While n % 3 == 0, set n = n / 3.

Finish

If final n is 1, answer true; otherwise false.

📜 Pseudocode

Pseudocode
function isPowerOfThree(n):
  if n <= 0:
    return false
  while n mod 3 == 0:
    n = n / 3
  return n == 1
1

Check one number

This checks whether 27 is a power of 3.

java
public class Main {
    static boolean isPowerOfThree(int n) {
        if (n <= 0) return false;
        while (n % 3 == 0) {
            n /= 3;
        }
        return n == 1;
    }

    public static void main(String[] args) {
        int number = 27;
        if (isPowerOfThree(number)) {
            System.out.println(number + " is a power of 3.");
        } else {
            System.out.println(number + " is not a power of 3.");
        }
    }
}
2

Power of 3 in range 1 to 20

This prints all powers of 3 from 1 to 20.

java
public class Main {
    static boolean isPowerOfThree(int n) {
        if (n <= 0) return false;
        while (n % 3 == 0) {
            n /= 3;
        }
        return n == 1;
    }

    public static void main(String[] args) {
        System.out.println("Power of 3 in the range 1 to 20:");
        for (int i = 1; i <= 20; i++) {
            if (isPowerOfThree(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

Optimization tips

  • Use integer operations only; avoid floating-point logarithms for exact checks.
  • Short-circuit early for n <= 0.
  • For repeated queries in a small bound, precompute known powers.

❓ FAQ

A number is a power of 3 if it can be written as 3^k for some whole number k (for example 1, 3, 9, 27).
Yes. 1 = 3^0.
If n is a power of 3, dividing by 3 keeps removing one factor of 3 and eventually reaches 1.
In this interview definition, powers of three are positive integers only.
You can, but floating-point precision can cause errors. Integer division loop is safer.
Divide-by-3 method is O(log n) time and O(1) extra space.

Input/output examples

Input nOutput
27Power of 3
45Not a power of 3
1Power of 3

Edge cases

Watch input validation and stop conditions.

n = 1

No division required

1 = 30, so it is a valid power of 3.

Multiples of 3

Not every multiple qualifies

6 becomes 2 after one divide, so answer is false.

Non-positive

Reject quickly

For interview constraints, values <= 0 are not powers of 3.

⏱️ Time and space complexity

MethodTimeExtra space
Repeated divide-by-3 loopO(log n)O(1)
Scan range 1..UO(U log U)O(1)

Summary

  • Idea: powers of 3 are 1, 3, 9, 27, … and contain only factor 3.
  • Code: reject n <= 0, divide by 3 while possible, then test n == 1.
  • Complexity: O(log n) time and O(1) extra space.
Did you know?

Powers of three are 1, 3, 9, 27, 81... and each term is 3 times the previous one.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful