Check if number is power of 4

Here given code implementation process.

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

// Check if that given number is power of 4 or not
void isPowerOfFour(int number)
{
	if ((number % 3 == 1) && (number & number - 1) == 0)
	{
		/*
		    The following cases are fulfilled to determine number is power of 4
		    1) When the number is divided by 3, its remainder should be 1 .
		    2) And it has only one active bit.
		*/
		printf(" Number %d is power of 4 \n", number);
	}
	else
	{
		printf(" Number %d is not power of 4 \n", number);
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Cases
	isPowerOfFour(16);
	isPowerOfFour(8);
	isPowerOfFour(40);
	isPowerOfFour(64);
	// 4 ^ 0 = 1
	isPowerOfFour(1);
	return 0;
}

input

 Number 16 is power of 4
 Number 8 is not power of 4
 Number 40 is not power of 4
 Number 64 is power of 4
 Number 1 is power of 4
/*
  Java Program for
  Check if number is power of 4
*/
public class Power
{
	// Check if that given number is power of 4 or not
	public void isPowerOfFour(int number)
	{
		if ((number % 3 == 1) && (number & number - 1) == 0)
		{
			//  The following cases are fulfilled to determine 
            //  number is power of 4
			//  1) When the number is divided by 3, its remainder should be 1 .
			//  2) And it has only one active bit.
			System.out.println(" Number " + number + " is power of 4 ");
		}
		else
		{
			System.out.println(" Number " + number + " is not power of 4 ");
		}
	}
	public static void main(String[] args)
	{
		Power task = new Power();
		// Test Cases
		task.isPowerOfFour(16);
		task.isPowerOfFour(8);
		task.isPowerOfFour(40);
		task.isPowerOfFour(64);
		// 4 ^ 0 = 1
		task.isPowerOfFour(1);
	}
}

input

 Number 16 is power of 4
 Number 8 is not power of 4
 Number 40 is not power of 4
 Number 64 is power of 4
 Number 1 is power of 4
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program for
  Check if number is power of 4
*/
class Power
{
	public:
		// Check if that given number is power of 4 or not
		void isPowerOfFour(int number)
		{
			if ((number % 3 == 1) && (number &number - 1) == 0)
			{
				//  The following cases are fulfilled to determine 
				//  number is power of 4
				//  1) When the number is divided by 3, its remainder should be 1 .
				//  2) And it has only one active bit.
				cout << " Number " << number << " is power of 4 " << endl;
			}
			else
			{
				cout << " Number " << number << " is not power of 4 " << endl;
			}
		}
};
int main()
{
	Power *task = new Power();
	// Test Cases
	task->isPowerOfFour(16);
	task->isPowerOfFour(8);
	task->isPowerOfFour(40);
	task->isPowerOfFour(64);
	// 4 ^ 0 = 1
	task->isPowerOfFour(1);
	return 0;
}

input

 Number 16 is power of 4
 Number 8 is not power of 4
 Number 40 is not power of 4
 Number 64 is power of 4
 Number 1 is power of 4
// Include namespace system
using System;
/*
  Csharp Program for
  Check if number is power of 4
*/
public class Power
{
	// Check if that given number is power of 4 or not
	public void isPowerOfFour(int number)
	{
		if ((number % 3 == 1) && (number & number - 1) == 0)
		{
			//  The following cases are fulfilled to determine 
			//  number is power of 4
			//  1) When the number is divided by 3, its remainder should be 1 .
			//  2) And it has only one active bit.
			Console.WriteLine(" Number " + number + " is power of 4 ");
		}
		else
		{
			Console.WriteLine(" Number " + number + " is not power of 4 ");
		}
	}
	public static void Main(String[] args)
	{
		Power task = new Power();
		// Test Cases
		task.isPowerOfFour(16);
		task.isPowerOfFour(8);
		task.isPowerOfFour(40);
		task.isPowerOfFour(64);
		// 4 ^ 0 = 1
		task.isPowerOfFour(1);
	}
}

input

 Number 16 is power of 4
 Number 8 is not power of 4
 Number 40 is not power of 4
 Number 64 is power of 4
 Number 1 is power of 4
<?php
/*
  Php Program for
  Check if number is power of 4
*/
class Power
{
	// Check if that given number is power of 4 or not
	public	function isPowerOfFour($number)
	{
		if (($number % 3 == 1) && ($number & $number - 1) == 0)
		{
			//  The following cases are fulfilled to determine 
			//  number is power of 4
			//  1) When the number is divided by 3, its remainder should be 1 .
			//  2) And it has only one active bit.
			echo " Number ".$number.
			" is power of 4 ".
			"\n";
		}
		else
		{
			echo " Number ".$number.
			" is not power of 4 ".
			"\n";
		}
	}
}

function main()
{
	$task = new Power();
	// Test Cases
	$task->isPowerOfFour(16);
	$task->isPowerOfFour(8);
	$task->isPowerOfFour(40);
	$task->isPowerOfFour(64);
	// 4 ^ 0 = 1
	$task->isPowerOfFour(1);
}
main();

input

 Number 16 is power of 4
 Number 8 is not power of 4
 Number 40 is not power of 4
 Number 64 is power of 4
 Number 1 is power of 4
/*
  Node JS Program for
  Check if number is power of 4
*/
class Power
{
	// Check if that given number is power of 4 or not
	isPowerOfFour(number)
	{
		if ((number % 3 == 1) && (number & number - 1) == 0)
		{
			//  The following cases are fulfilled to determine 
			//  number is power of 4
			//  1) When the number is divided by 3, its remainder should be 1 .
			//  2) And it has only one active bit.
			console.log(" Number " + number + " is power of 4 ");
		}
		else
		{
			console.log(" Number " + number + " is not power of 4 ");
		}
	}
}

function main()
{
	var task = new Power();
	// Test Cases
	task.isPowerOfFour(16);
	task.isPowerOfFour(8);
	task.isPowerOfFour(40);
	task.isPowerOfFour(64);
	// 4 ^ 0 = 1
	task.isPowerOfFour(1);
}
main();

input

 Number 16 is power of 4
 Number 8 is not power of 4
 Number 40 is not power of 4
 Number 64 is power of 4
 Number 1 is power of 4
#  Python 3 Program for
#  Check if number is power of 4
class Power :
	#  Check if that given number is power of 4 or not
	def isPowerOfFour(self, number) :
		if ((number % 3 == 1) and(number & number - 1) == 0) :
			#   The following cases are fulfilled to determine 
			#   number is power of 4
			#   1) When the number is divided by 3, its remainder should be 1 .
			#   2) And it has only one active bit.
			print(" Number ", number ," is power of 4 ")
		else :
			print(" Number ", number ," is not power of 4 ")
		
	

def main() :
	task = Power()
	#  Test Cases
	task.isPowerOfFour(16)
	task.isPowerOfFour(8)
	task.isPowerOfFour(40)
	task.isPowerOfFour(64)
	#  4 ^ 0 = 1
	task.isPowerOfFour(1)

if __name__ == "__main__": main()

input

 Number  16  is power of 4
 Number  8  is not power of 4
 Number  40  is not power of 4
 Number  64  is power of 4
 Number  1  is power of 4
#  Ruby Program for
#  Check if number is power of 4
class Power 
	#  Check if that given number is power of 4 or not
	def isPowerOfFour(number) 
		if ((number % 3 == 1) && (number & number - 1) == 0) 
			#   The following cases are fulfilled to determine 
			#   number is power of 4
			#   1) When the number is divided by 3, its remainder should be 1 .
			#   2) And it has only one active bit.
			print(" Number ", number ," is power of 4 ", "\n")
		else 
			print(" Number ", number ," is not power of 4 ", "\n")
		end

	end

end

def main() 
	task = Power.new()
	#  Test Cases
	task.isPowerOfFour(16)
	task.isPowerOfFour(8)
	task.isPowerOfFour(40)
	task.isPowerOfFour(64)
	#  4 ^ 0 = 1
	task.isPowerOfFour(1)
end

main()

input

 Number 16 is power of 4 
 Number 8 is not power of 4 
 Number 40 is not power of 4 
 Number 64 is power of 4 
 Number 1 is power of 4 
/*
  Scala Program for
  Check if number is power of 4
*/
class Power()
{
	// Check if that given number is power of 4 or not
	def isPowerOfFour(number: Int): Unit = {
		if ((number % 3 == 1) && (number & number - 1) == 0)
		{
			//  The following cases are fulfilled to determine 
			//  number is power of 4
			//  1) When the number is divided by 3, its remainder should be 1 .
			//  2) And it has only one active bit.
			println(" Number " + number + " is power of 4 ");
		}
		else
		{
			println(" Number " + number + " is not power of 4 ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Power = new Power();
		// Test Cases
		task.isPowerOfFour(16);
		task.isPowerOfFour(8);
		task.isPowerOfFour(40);
		task.isPowerOfFour(64);
		// 4 ^ 0 = 1
		task.isPowerOfFour(1);
	}
}

input

 Number 16 is power of 4
 Number 8 is not power of 4
 Number 40 is not power of 4
 Number 64 is power of 4
 Number 1 is power of 4
/*
  Swift 4 Program for
  Check if number is power of 4
*/
class Power
{
	// Check if that given number is power of 4 or not
	func isPowerOfFour(_ number: Int)
	{
		if ((number % 3 == 1) && (number & number - 1) == 0)
		{
			//  The following cases are fulfilled to determine 
			//  number is power of 4
			//  1) When the number is divided by 3, its remainder should be 1 .
			//  2) And it has only one active bit.
			print(" Number ", number ," is power of 4 ");
		}
		else
		{
			print(" Number ", number ," is not power of 4 ");
		}
	}
}
func main()
{
	let task: Power = Power();
	// Test Cases
	task.isPowerOfFour(16);
	task.isPowerOfFour(8);
	task.isPowerOfFour(40);
	task.isPowerOfFour(64);
	// 4 ^ 0 = 1
	task.isPowerOfFour(1);
}
main();

input

 Number  16  is not power of 4
 Number  8  is not power of 4
 Number  40  is not power of 4
 Number  64  is not power of 4
 Number  1  is power of 4
/*
  Kotlin Program for
  Check if number is power of 4
*/
class Power
{
	// Check if that given number is power of 4 or not
	fun isPowerOfFour(number: Int): Unit
	{
		if ((number % 3 == 1) && (number and number - 1) == 0)
		{
			//  The following cases are fulfilled to determine 
			//  number is power of 4
			//  1) When the number is divided by 3, its remainder should be 1 .
			//  2) And it has only one active bit.
			println(" Number " + number + " is power of 4 ");
		}
		else
		{
			println(" Number " + number + " is not power of 4 ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Power = Power();
	// Test Cases
	task.isPowerOfFour(16);
	task.isPowerOfFour(8);
	task.isPowerOfFour(40);
	task.isPowerOfFour(64);
	// 4 ^ 0 = 1
	task.isPowerOfFour(1);
}

input

 Number 16 is power of 4
 Number 8 is not power of 4
 Number 40 is not power of 4
 Number 64 is power of 4
 Number 1 is power of 4


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