Coding Challenge Day-9: Problem 2: Find whether a given number is a power of three

Hello! So I’m doing a 30 day coding challenge where I solve around 3-5 questions per day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 9: Problem 2! Be sure to post your answers, queries etc in the comments!

Problem: Find whether a given number is a power of three

Sample input: 9
Output: 9 is a power of three

Sample input: 6
Output: 6 is not a power of three

Sample input: 10
Output: 10 is not a power of three

Solutions:

package misc;

public class Program001power_of_three {
	
	public static void main (String [] args){
		int number= 10;
		
		/* The maximum power of 3 value that  
	       integer can hold is 1162261467 ( 3^19 ) .*/
		if(1162261467%number==0)
			System.out.println(number + " is a power of three ");
		else
			System.out.println(number + " is not a power of three");
		
	}

}

Download Code

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: