Check if number is power of 3

Here given code implementation process.

// C program for 
// Check if number is power of 3
#include <stdio.h>

// Check if that given number is power of 3 or not
void isPowerOfThree(int number)
{
	if (3486784401u % number == 0)
	{
		/*
		  Highest power of 3 in unsigned integer
		  3486784401 (3^20)
		  That is divisible by 3 and remainder is zero
		*/
		printf(" Number %d is power of 3 \n", number);
	}
	else
	{
		printf(" Number %d is not power of 3 \n", number);
	}
}
int main(int argc, char const *argv[])
{
	// Test Case
	isPowerOfThree(81);
	isPowerOfThree(18);
	isPowerOfThree(27);
	isPowerOfThree(30);
	isPowerOfThree(-27);
	return 0;
}

input

 Number 81 is power of 3
 Number 18 is not power of 3
 Number 27 is power of 3
 Number 30 is not power of 3
 Number -27 is not power of 3
/*
  Java Program for
  Check if number is power of 3
*/
public class Power
{
	// Check whether given number is power of 3 or not
	public void isPowerOfThree(long number)
	{
		if (3486784401L % number == 0)
		{
			/*
			  Highest power of 3 in unsigned integer
			  3486784401 (3^20)
			  That is divisible by 3 and remainder is zero
			*/
			System.out.println(" Number " + number + " is power of 3 ");
		}
		else
		{
			System.out.println(" Number " + number + " is not power of 3 ");
		}
	}
	public static void main(String[] args)
	{
		Power task = new Power();
		// Test Case
		task.isPowerOfThree(81);
		task.isPowerOfThree(18);
		task.isPowerOfThree(27);
		task.isPowerOfThree(30);
		// 3 ^ 0 = 1
		task.isPowerOfThree(1);
		task.isPowerOfThree(-27);
	}
}

input

 Number 81 is power of 3
 Number 18 is not power of 3
 Number 27 is power of 3
 Number 30 is not power of 3
 Number 1 is power of 3
 Number -27 is power of 3
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Check if number is power of 3
*/
class Power
{
	public:
		// Check whether given number is power of 3 or not
		void isPowerOfThree(long number)
		{
			if (3486784401L % number == 0)
			{
				/*
				  Highest power of 3 in unsigned integer
				  3486784401 (3^20)
				  That is divisible by 3 and remainder is zero
				*/
				cout << " Number " << number << " is power of 3 " << endl;
			}
			else
			{
				cout << " Number " << number << " is not power of 3 " << endl;
			}
		}
};
int main()
{
	Power *task = new Power();
	// Test Case
	task->isPowerOfThree(81);
	task->isPowerOfThree(18);
	task->isPowerOfThree(27);
	task->isPowerOfThree(30);
	// 3 ^ 0 = 1
	task->isPowerOfThree(1);
	task->isPowerOfThree(-27);
	return 0;
}

input

 Number 81 is power of 3
 Number 18 is not power of 3
 Number 27 is power of 3
 Number 30 is not power of 3
 Number 1 is power of 3
 Number -27 is power of 3
// Include namespace system
using System;
/*
  Csharp Program for
  Check if number is power of 3
*/
public class Power
{
	// Check whether given number is power of 3 or not
	public void isPowerOfThree(long number)
	{
		if (3486784401L % number == 0)
		{
			/*
			  Highest power of 3 in unsigned integer
			  3486784401 (3^20)
			  That is divisible by 3 and remainder is zero
			*/
			Console.WriteLine(" Number " + number + " is power of 3 ");
		}
		else
		{
			Console.WriteLine(" Number " + number + " is not power of 3 ");
		}
	}
	public static void Main(String[] args)
	{
		Power task = new Power();
		// Test Case
		task.isPowerOfThree(81);
		task.isPowerOfThree(18);
		task.isPowerOfThree(27);
		task.isPowerOfThree(30);
		// 3 ^ 0 = 1
		task.isPowerOfThree(1);
		task.isPowerOfThree(-27);
	}
}

input

 Number 81 is power of 3
 Number 18 is not power of 3
 Number 27 is power of 3
 Number 30 is not power of 3
 Number 1 is power of 3
 Number -27 is power of 3
<?php
/*
  Php Program for
  Check if number is power of 3
*/
class Power
{
	// Check whether given number is power of 3 or not
	public	function isPowerOfThree($number)
	{
		if (3486784401 % $number == 0)
		{
			/*
			  Highest power of 3 in unsigned integer
			  3486784401 (3^20)
			  That is divisible by 3 and remainder is zero
			*/
			echo " Number ".$number.
			" is power of 3 ".
			"\n";
		}
		else
		{
			echo " Number ".$number.
			" is not power of 3 ".
			"\n";
		}
	}
}

function main()
{
	$task = new Power();
	// Test Case
	$task->isPowerOfThree(81);
	$task->isPowerOfThree(18);
	$task->isPowerOfThree(27);
	$task->isPowerOfThree(30);
	// 3 ^ 0 = 1
	$task->isPowerOfThree(1);
	$task->isPowerOfThree(-27);
}
main();

input

 Number 81 is power of 3
 Number 18 is not power of 3
 Number 27 is power of 3
 Number 30 is not power of 3
 Number 1 is power of 3
 Number -27 is power of 3
/*
  Node JS Program for
  Check if number is power of 3
*/
class Power
{
	// Check whether given number is power of 3 or not
	isPowerOfThree(number)
	{
		if (3486784401 % number == 0)
		{
			/*
			  Highest power of 3 in unsigned integer
			  3486784401 (3^20)
			  That is divisible by 3 and remainder is zero
			*/
			console.log(" Number " + number + " is power of 3 ");
		}
		else
		{
			console.log(" Number " + number + " is not power of 3 ");
		}
	}
}

function main()
{
	var task = new Power();
	// Test Case
	task.isPowerOfThree(81);
	task.isPowerOfThree(18);
	task.isPowerOfThree(27);
	task.isPowerOfThree(30);
	// 3 ^ 0 = 1
	task.isPowerOfThree(1);
	task.isPowerOfThree(-27);
}
main();

input

 Number 81 is power of 3
 Number 18 is not power of 3
 Number 27 is power of 3
 Number 30 is not power of 3
 Number 1 is power of 3
 Number -27 is power of 3
#  Python 3 Program for
#  Check if number is power of 3
class Power :
	#  Check whether given number is power of 3 or not
	def isPowerOfThree(self, number) :
		if (3486784401 % number == 0) :
			#  Highest power of 3 in unsigned integer
			#  3486784401 (3^20)
			#  That is divisible by 3 and remainder is zero
			print(" Number", number ," is power of 3 ")
		else :
			print(" Number", number ," is not power of 3 ")
		
	

def main() :
	task = Power()
	#  Test Case
	task.isPowerOfThree(81)
	task.isPowerOfThree(18)
	task.isPowerOfThree(27)
	task.isPowerOfThree(30)
	#  3 ^ 0 = 1
	task.isPowerOfThree(1)
	task.isPowerOfThree(-27)

if __name__ == "__main__": main()

input

 Number 81  is power of 3
 Number 18  is not power of 3
 Number 27  is power of 3
 Number 30  is not power of 3
 Number 1  is power of 3
 Number -27  is power of 3
#  Ruby Program for
#  Check if number is power of 3
class Power 
	#  Check whether given number is power of 3 or not
	def isPowerOfThree(number) 
		if (3486784401 % number == 0) 
			#  Highest power of 3 in unsigned integer
			#  3486784401 (3^20)
			#  That is divisible by 3 and remainder is zero
			print(" Number ", number ," is power of 3 ", "\n")
		else 
			print(" Number ", number ," is not power of 3 ", "\n")
		end

	end

end

def main() 
	task = Power.new()
	#  Test Case
	task.isPowerOfThree(81)
	task.isPowerOfThree(18)
	task.isPowerOfThree(27)
	task.isPowerOfThree(30)
	#  3 ^ 0 = 1
	task.isPowerOfThree(1)
	task.isPowerOfThree(-27)
end

main()

input

 Number 81 is power of 3 
 Number 18 is not power of 3 
 Number 27 is power of 3 
 Number 30 is not power of 3 
 Number 1 is power of 3 
 Number -27 is power of 3 
/*
  Scala Program for
  Check if number is power of 3
*/
class Power()
{
	// Check whether given number is power of 3 or not
	def isPowerOfThree(number: Long): Unit = {
		if (3486784401L % number == 0)
		{
			/*
			  Highest power of 3 in unsigned integer
			  3486784401 (3^20)
			  That is divisible by 3 and remainder is zero
			*/
			println(" Number " + number + " is power of 3 ");
		}
		else
		{
			println(" Number " + number + " is not power of 3 ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Power = new Power();
		// Test Case
		task.isPowerOfThree(81);
		task.isPowerOfThree(18);
		task.isPowerOfThree(27);
		task.isPowerOfThree(30);
		// 3 ^ 0 = 1
		task.isPowerOfThree(1);
		task.isPowerOfThree(-27);
	}
}

input

 Number 81 is power of 3
 Number 18 is not power of 3
 Number 27 is power of 3
 Number 30 is not power of 3
 Number 1 is power of 3
 Number -27 is power of 3
/*
  Swift 4 Program for
  Check if number is power of 3
*/
class Power
{
	// Check whether given number is power of 3 or not
	func isPowerOfThree(_ number: Int)
	{
		if (3486784401 % number == 0)
		{
			/*
			  Highest power of 3 in unsigned integer
			  3486784401 (3^20)
			  That is divisible by 3 and remainder is zero
			*/
			print(" Number ", number ," is power of 3 ");
		}
		else
		{
			print(" Number ", number ," is not power of 3 ");
		}
	}
}
func main()
{
	let task: Power = Power();
	// Test Case
	task.isPowerOfThree(81);
	task.isPowerOfThree(18);
	task.isPowerOfThree(27);
	task.isPowerOfThree(30);
	// 3 ^ 0 = 1
	task.isPowerOfThree(1);
	task.isPowerOfThree(-27);
}
main();

input

 Number  81  is power of 3
 Number  18  is not power of 3
 Number  27  is power of 3
 Number  30  is not power of 3
 Number  1  is power of 3
 Number  -27  is power of 3
/*
  Kotlin Program for
  Check if number is power of 3
*/
class Power
{
	// Check whether given number is power of 3 or not
	fun isPowerOfThree(number: Long): Unit
	{
       
		if ((3486784401 % number) == 0L)
		{
			/*
			  Highest power of 3 in unsigned integer
			  3486784401 (3^20)
			  That is divisible by 3 and remainder is zero
			*/
			println(" Number " + number + " is power of 3 ");
		}
		else
		{
			println(" Number " + number + " is not power of 3 ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Power = Power();
	// Test Case
	task.isPowerOfThree(81);
	task.isPowerOfThree(18);
	task.isPowerOfThree(27);
	task.isPowerOfThree(30);
	// 3 ^ 0 = 1
	task.isPowerOfThree(1);
	task.isPowerOfThree(-27);
}

input

 Number 81 is power of 3
 Number 18 is not power of 3
 Number 27 is power of 3
 Number 30 is not power of 3
 Number 1 is power of 3
 Number -27 is power of 3


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved