Check Power of 3 in C#

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • What a power of 3 means: n = 3^k for a whole number k.
  • How to test by repeatedly dividing by 3 until you reach 1 or find a remainder.
  • How to check 27 and list all powers of 3 from 1 to 20.

What is a power of 3?

A positive integer is a power of 3 when it equals 3 multiplied by itself k times. Examples: 1, 3, 9, 27, 81. Values like 2, 6, 10, 12 are not powers of 3.

Factor view

27 = 3 × 3 × 3 = 3³.

Divide loop

27 → 9 → 3 → 1 — every step divides cleanly by 3.

Range 1–20

Outputs 1 3 9 — only three fit in that range.

Prerequisites

Modulo %, integer division, and simple loops.

  • You know that n % 3 == 0 means n is divisible by 3.
  • You can read a while loop and a for loop in C#.

Understanding the concept

A power of 3 contains only the prime factor 3. Peel off one factor at a time: if 27 divides by 3 to give 9, then 3, then 1, it is . If you ever get a remainder, stop — the number is not a power of 3.

Intuition examples

27 = 3³ (power of 3). 12 = 3 × 4 has a factor of 4, so dividing by 3 once leaves 4, which is not divisible by 3. 1 = 3&sup0; counts as a power of 3.

Live preview

Enter an integer and watch each divide-by-3 step until the verdict.

Live result
Press “Run check”.

Algorithm

Require positive n

If n <= 0, return false.

Divide by 3 while possible

While n > 1, if n % 3 != 0 return false; otherwise set n = n / 3.

Return result

If the loop finishes with n == 1, the original value was a power of 3.

📜 Pseudocode

Pseudocode
function isPowerOfThree(n):
    if n <= 0:
        return false
    while n > 1:
        if n % 3 != 0:
            return false
        n = n / 3
    return true
1

Check one number

Reference program: tests whether 27 is a power of 3 by peeling off factors of 3.

c#
using System;

class Program
{
    static bool IsPowerOfThree(int n)
    {
        if (n <= 0)
            return false;

        while (n > 1)
        {
            if (n % 3 != 0)
                return false;

            n /= 3;
        }

        return true;
    }

    static void Main()
    {
        int number = 27;

        if (IsPowerOfThree(number))
            Console.WriteLine($"{number} is a power of 3.");
        else
            Console.WriteLine($"{number} is not a power of 3.");
    }
}
2

Powers of 3 from 1 to 20

Reuses IsPowerOfThree inside a range loop — matches the reference range output.

c#
using System;

class Program
{
    static bool IsPowerOfThree(int n)
    {
        if (n <= 0)
            return false;

        while (n > 1)
        {
            if (n % 3 != 0)
                return false;

            n /= 3;
        }

        return true;
    }

    static void Main()
    {
        Console.WriteLine("Power of 3 in the range 1 to 20:");

        for (int i = 1; i <= 20; i++)
        {
            if (IsPowerOfThree(i))
                Console.Write($"{i} ");
        }

        Console.WriteLine();
    }
}

Optimization and alternatives

Divide loop (preferred for learning). Clear, correct, and runs in O(log n) time.

32-bit shortcut. The largest power of 3 in a signed 32-bit int is 3^19 = 1162261467. For positive n, 1162261467 % n == 0 also identifies powers of 3 — handy in interviews after you understand the loop.

Logarithm check. Test whether Math.Log(n, 3) is (nearly) an integer — watch out for floating-point rounding on large values.

❓ FAQ

A positive integer n is a power of 3 when n = 3^k for some whole number k — examples: 1, 3, 9, 27, 81.
Yes. 1 = 3^0.
Every power of 3 is built only from the factor 3. If dividing by 3 ever leaves a remainder, n cannot be 3^k.
No simple bit pattern exists. Use the divide-by-3 loop or a math shortcut for fixed ranges.
You can test whether log(n)/log(3) is an integer, but floating-point rounding makes the loop safer for beginners.
The divide-by-3 loop runs O(log n) times because you remove one factor of 3 per iteration.

🔄 Input / output examples

Input nDivide stepsResult
2727 → 9 → 3 → 1Power of 3
1212 → 4 (stops: remainder)Not a power of 3
1Already 1Power of 3
0Rejected by guardNot a power of 3

Edge cases and pitfalls

One

n = 1

1 = 3^0 — the loop never runs and returns true.

Zero

n = 0

Return false immediately; zero is not a power of 3.

Negatives

Not powers of three

Return false for n <= 0.

⏱️ Time and space complexity

MethodTimeExtra space
Divide-by-3 loopO(log n)O(1)
Modulo with 1162261467O(1)O(1)
Range 1..RO(R log R)O(1)

Summary

  • Powers of three are 3^k — only the factor 3 appears.
  • Divide by 3 while possible; any remainder means false.
  • From 1 to 20, only 1, 3, 9 qualify.
Did you know?

Unlike powers of 2, powers of 3 have no simple one-bit binary pattern. The classic interview-friendly approach is to keep dividing by 3 until you reach 1 or hit a remainder.

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