Power of Three - The Coding Shala

Home >> Programming >> Power of Three

 In this post, we will learn how to find if the Given number is Power of Three or not, and will implement its solution in Java.

Power of Three Problem

Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three if there exists an integer x such that n == 3^x.

Example 1:
Input: n = 27
Output: true

Example 2:
Input: n = 0
Output: false

Example 3:
Input: n = 9
Output: true

Power of Three Java Solution

Approach 1

Using loop.

Java Program: 

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

Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.


Popular Posts from this Blog

Anti Diagonals - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala