Check whether given number is power of 2 are efficiently

Here given code implementation process.

// C Program for
// Check whether given number is power of 2 are efficiently
#include <stdio.h>

// Detect that given number are part of power of 2 or not
void isPowerTwo(int number)
{
	if (number <= 0 || (number & (number - 1)) != 0)
	{
		printf("%d Is not a power of two\n", number);
	}
	else
	{
		printf("%d Is a power of two\n", number);
	}
}
int main()
{
	// Test Case
	isPowerTwo(4);
	isPowerTwo(7);
	isPowerTwo(32);
	isPowerTwo(63);
	isPowerTwo(3542);
	isPowerTwo(128);
	return 0;
}

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
// Java program for
// Check whether given number is power of 2 are efficiently
public class PowerTwo
{
	// Detect that given number are part of power of 2 or not
	public void isPowerTwo(int number)
	{
		if (number <= 0 || (number & (number - 1)) != 0)
		{
			System.out.print(number + " Is not a power of two\n");
		}
		else
		{
			System.out.print(number + " Is a power of two\n");
		}
	}
	public static void main(String[] args)
	{
		PowerTwo task = new PowerTwo();
		// Test Case
		task.isPowerTwo(4);
		task.isPowerTwo(7);
		task.isPowerTwo(32);
		task.isPowerTwo(63);
		task.isPowerTwo(3542);
		task.isPowerTwo(128);
	}
}

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Check whether given number is power of 2 are efficiently
class PowerTwo
{
	public:
		// Detect that given number are part of power of 2 or not
		void isPowerTwo(int number)
		{
			if (number <= 0 || (number &(number - 1)) != 0)
			{
				cout << number << " Is not a power of two\n";
			}
			else
			{
				cout << number << " Is a power of two\n";
			}
		}
};
int main()
{
	PowerTwo *task = new PowerTwo();
	// Test Case
	task->isPowerTwo(4);
	task->isPowerTwo(7);
	task->isPowerTwo(32);
	task->isPowerTwo(63);
	task->isPowerTwo(3542);
	task->isPowerTwo(128);
	return 0;
}

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
package main
import "fmt"
// Go program for
// Check whether given number is power of 2 are efficiently
type PowerTwo struct {}
func getPowerTwo() * PowerTwo {
	var me *PowerTwo = &PowerTwo {}
	return me
}
// Detect that given number are part of power of 2 or not
func(this PowerTwo) isPowerTwo(number int) {
	if number <= 0 || (number & (number - 1)) != 0 {
		fmt.Print(number, " Is not a power of two\n")
	} else {
		fmt.Print(number, " Is a power of two\n")
	}
}
func main() {
	var task * PowerTwo = getPowerTwo()
	// Test Case
	task.isPowerTwo(4)
	task.isPowerTwo(7)
	task.isPowerTwo(32)
	task.isPowerTwo(63)
	task.isPowerTwo(3542)
	task.isPowerTwo(128)
}

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
// Include namespace system
using System;
// Csharp program for
// Check whether given number is power of 2 are efficiently
public class PowerTwo
{
	// Detect that given number are part of power of 2 or not
	public void isPowerTwo(int number)
	{
		if (number <= 0 || (number & (number - 1)) != 0)
		{
			Console.Write(number + " Is not a power of two\n");
		}
		else
		{
			Console.Write(number + " Is a power of two\n");
		}
	}
	public static void Main(String[] args)
	{
		PowerTwo task = new PowerTwo();
		// Test Case
		task.isPowerTwo(4);
		task.isPowerTwo(7);
		task.isPowerTwo(32);
		task.isPowerTwo(63);
		task.isPowerTwo(3542);
		task.isPowerTwo(128);
	}
}

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
<?php
// Php program for
// Check whether given number is power of 2 are efficiently
class PowerTwo
{
	// Detect that given number are part of power of 2 or not
	public	function isPowerTwo($number)
	{
		if ($number <= 0 || ($number & ($number - 1)) != 0)
		{
			echo($number.
				" Is not a power of two\n");
		}
		else
		{
			echo($number.
				" Is a power of two\n");
		}
	}
}

function main()
{
	$task = new PowerTwo();
	// Test Case
	$task->isPowerTwo(4);
	$task->isPowerTwo(7);
	$task->isPowerTwo(32);
	$task->isPowerTwo(63);
	$task->isPowerTwo(3542);
	$task->isPowerTwo(128);
}
main();

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
// Node JS program for
// Check whether given number is power of 2 are efficiently
class PowerTwo
{
	// Detect that given number are part of power of 2 or not
	isPowerTwo(number)
	{
		if (number <= 0 || (number & (number - 1)) != 0)
		{
			process.stdout.write(number + " Is not a power of two\n");
		}
		else
		{
			process.stdout.write(number + " Is a power of two\n");
		}
	}
}

function main()
{
	var task = new PowerTwo();
	// Test Case
	task.isPowerTwo(4);
	task.isPowerTwo(7);
	task.isPowerTwo(32);
	task.isPowerTwo(63);
	task.isPowerTwo(3542);
	task.isPowerTwo(128);
}
main();

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
#  Python 3 program for
#  Check whether given number is power of 2 are efficiently
class PowerTwo :
	#  Detect that given number are part of power of 2 or not
	def isPowerTwo(self, number) :
		if (number <= 0 or(number & (number - 1)) != 0) :
			print(number ," Is not a power of two")
		else :
			print(number ," Is a power of two")
		
	

def main() :
	task = PowerTwo()
	#  Test Case
	task.isPowerTwo(4)
	task.isPowerTwo(7)
	task.isPowerTwo(32)
	task.isPowerTwo(63)
	task.isPowerTwo(3542)
	task.isPowerTwo(128)

if __name__ == "__main__": main()

Output

4  Is a power of two
7  Is not a power of two
32  Is a power of two
63  Is not a power of two
3542  Is not a power of two
128  Is a power of two
#  Ruby program for
#  Check whether given number is power of 2 are efficiently
class PowerTwo 
	#  Detect that given number are part of power of 2 or not
	def isPowerTwo(number) 
		if (number <= 0 || (number & (number - 1)) != 0) 
			print(number ," Is not a power of two\n")
		else
 
			print(number ," Is a power of two\n")
		end

	end

end

def main() 
	task = PowerTwo.new()
	#  Test Case
	task.isPowerTwo(4)
	task.isPowerTwo(7)
	task.isPowerTwo(32)
	task.isPowerTwo(63)
	task.isPowerTwo(3542)
	task.isPowerTwo(128)
end

main()

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
// Scala program for
// Check whether given number is power of 2 are efficiently
class PowerTwo()
{
	// Detect that given number are part of power of 2 or not
	def isPowerTwo(number: Int): Unit = {
		if (number <= 0 || (number & (number - 1)) != 0)
		{
			print(""+ number + " Is not a power of two\n");
		}
		else
		{
			print(""+ number + " Is a power of two\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PowerTwo = new PowerTwo();
		// Test Case
		task.isPowerTwo(4);
		task.isPowerTwo(7);
		task.isPowerTwo(32);
		task.isPowerTwo(63);
		task.isPowerTwo(3542);
		task.isPowerTwo(128);
	}
}

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two
// Swift 4 program for
// Check whether given number is power of 2 are efficiently
class PowerTwo
{
	// Detect that given number are part of power of 2 or not
	func isPowerTwo(_ number: Int)
	{
		if (number <= 0 || (number & (number - 1))  != 0)
		{
			print(number ," Is not a power of two");
		}
		else
		{
			print(number ," Is a power of two");
		}
	}
}
func main()
{
	let task: PowerTwo = PowerTwo();
	// Test Case
	task.isPowerTwo(4);
	task.isPowerTwo(7);
	task.isPowerTwo(32);
	task.isPowerTwo(63);
	task.isPowerTwo(3542);
	task.isPowerTwo(128);
}
main();

Output

4  Is a power of two
7  Is not a power of two
32  Is a power of two
63  Is not a power of two
3542  Is not a power of two
128  Is a power of two
// Kotlin program for
// Check whether given number is power of 2 are efficiently
class PowerTwo
{
	// Detect that given number are part of power of 2 or not
	fun isPowerTwo(number: Int): Unit
	{
		if (number <= 0 || (number and(number - 1)) != 0)
		{
			print(""+ number + " Is not a power of two\n");
		}
		else
		{
			print(""+ number + " Is a power of two\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PowerTwo = PowerTwo();
	// Test Case
	task.isPowerTwo(4);
	task.isPowerTwo(7);
	task.isPowerTwo(32);
	task.isPowerTwo(63);
	task.isPowerTwo(3542);
	task.isPowerTwo(128);
}

Output

4 Is a power of two
7 Is not a power of two
32 Is a power of two
63 Is not a power of two
3542 Is not a power of two
128 Is a power of two


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