Check if number is power of 2

Here given code implementation process.

//C Program
//Check if number is power of 2
#include <stdio.h>

//Detect that given number are part of power of 2 or not
void is_power_two(int number)
{
  int status = 0;
  
  int counter =1;

  while(status==0 && (2 << counter) <=number)
  {

    if((2 << counter)==number)
    {
      //When result are exist in power of 2
      status=1;
    } 
    counter++;
  }
  
  if(status==0)
  {
    printf("%d Is not a power of two\n",number);
  }
  else
  {
    printf("%d Is a power of two\n",number);
  }

}
int main()
{
  //Test Cases
  is_power_two(4);
  is_power_two(7);
  is_power_two(32);
  is_power_two(63);
  is_power_two(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
128 Is a power of two
/*
  C++ Program
  Check if given number is stable or unstable 
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public:
		//Detect that given number are part of power of 2 or not
		void is_power_two(int number)
		{
			bool status = false;
			int counter = 1;
			while (status == false && (2 << counter) <= number)
			{
				if ((2 << counter) == number)
				{
					//When result are exist in power of 2
					status = true;
				}
				counter++;
			}
			if (status == false)
			{
				cout << "" << number << " Is not a power of two\n";
			}
			else
			{
				cout << "" << number << " Is a power of two\n";
			}
		}
};
int main()
{
	MyNumber obj =  MyNumber();
	//Test Cases
	obj.is_power_two(4);
	obj.is_power_two(7);
	obj.is_power_two(32);
	obj.is_power_two(63);
	obj.is_power_two(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
128 Is a power of two
/*
  Java Program
  Check if number is power of 2
*/
public class MyNumber 
{
  //Detect that given number are part of power of 2 or not
  public void is_power_two(int number)
  {
    boolean status = false;
    int counter = 1;
    while (status == false && (2 << counter) <= number)
    {
      if ((2 << counter) == number)
      {
        //When result are exist in power of 2
        status = true;
      }
      counter++;
    }
    if (status == false)
    {
      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) 
  {
    MyNumber obj = new MyNumber();

    //Test Cases
    obj.is_power_two(4);
    obj.is_power_two(7);
    obj.is_power_two(32);
    obj.is_power_two(63);
    obj.is_power_two(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
 128  Is a power of two
/*
  C# Program
  Check if given number is stable or unstable 
*/
using System;
public class MyNumber
{
	//Detect that given number are part of power of 2 or not
	public void is_power_two(int number)
	{
		Boolean status = false;
		int counter = 1;
		while (status == false && (2 << counter) <= number)
		{
			if ((2 << counter) == number)
			{
				//When result are exist in power of 2
				status = true;
			}
			counter++;
		}
		if (status == false)
		{
			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)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		obj.is_power_two(4);
		obj.is_power_two(7);
		obj.is_power_two(32);
		obj.is_power_two(63);
		obj.is_power_two(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
128 Is a power of two
<?php
/*
  Php Program
  Check if given number is stable or unstable 
*/
class MyNumber
{
	//Detect that given number are part of power of 2 or not
	public 	function is_power_two($number)
	{
		$status = false;
		$counter = 1;
		while ($status == false && (2 << $counter) <= $number)
		{
			if ((2 << $counter) == $number)
			{
				//When result are exist in power of 2
				$status = true;
			}
			$counter++;
		}
		if ($status == false)
		{
			echo("". $number ." Is not a power of two\n");
		}
		else
		{
			echo("". $number ." Is a power of two\n");
		}
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Cases
	$obj->is_power_two(4);
	$obj->is_power_two(7);
	$obj->is_power_two(32);
	$obj->is_power_two(63);
	$obj->is_power_two(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
128 Is a power of two
/*
  Node Js Program
  Check if given number is stable or unstable 
*/
class MyNumber
{
	//Detect that given number are part of power of 2 or not
	is_power_two(number)
	{
		var status = false;
		var counter = 1;
		while (status == false && (2 << counter) <= number)
		{
			if ((2 << counter) == number)
			{
				//When result are exist in power of 2
				status = true;
			}
			counter++;
		}
		if (status == false)
		{
			process.stdout.write("" + number + " Is not a power of two\n");
		}
		else
		{
			process.stdout.write("" + number + " Is a power of two\n");
		}
	}
}

function main(args)
{
	var obj = new MyNumber();
	//Test Cases
	obj.is_power_two(4);
	obj.is_power_two(7);
	obj.is_power_two(32);
	obj.is_power_two(63);
	obj.is_power_two(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
128 Is a power of two
#   Python 3 Program
#   Check if given number is stable or unstable 

class MyNumber :
	# Detect that given number are part of power of 2 or not
	def is_power_two(self, number) :
		status = False
		counter = 1
		while (status == False and(2 << counter) <= number) :
			if ((2 << counter) == number) :
				# When result are exist in power of 2
				status = True
			
			counter += 1
		
		if (status == False) :
			print("", number ," Is not a power of two\n", end = "")
		else :
			print("", number ," Is a power of two\n", end = "")
		
	

def main() :
	obj = MyNumber()
	# Test Cases
	obj.is_power_two(4)
	obj.is_power_two(7)
	obj.is_power_two(32)
	obj.is_power_two(63)
	obj.is_power_two(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
 128  Is a power of two
#   Ruby Program
#   Check if given number is stable or unstable 

class MyNumber

	# Detect that given number are part of power of 2 or not
	def is_power_two(number)
	
		status = false
		counter = 1
		while (status == false && (2 << counter) <= number)
		
			if ((2 << counter) == number)
			
				# When result are exist in power of 2
				status = true
			end
			counter += 1
		end
		if (status == false)
		
			print("", number ," Is not a power of two\n")
		else
		
			print("", number ," Is a power of two\n")
		end
	end
end
def main()

	obj = MyNumber.new()
	# Test Cases
	obj.is_power_two(4)
	obj.is_power_two(7)
	obj.is_power_two(32)
	obj.is_power_two(63)
	obj.is_power_two(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
128 Is a power of two
/*
  Scala Program
  Check if given number is stable or unstable 
*/
class MyNumber
{
	//Detect that given number are part of power of 2 or not
	def is_power_two(number: Int): Unit = {
		var status: Boolean = false;
		var counter: Int = 1;
		while (status == false && (2 << counter) <= number)
		{
			if ((2 << counter) == number)
			{
				//When result are exist in power of 2
				status = true;
			}
			counter += 1;
		}
		if (status == false)
		{
			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 obj: MyNumber = new MyNumber();
		//Test Cases
		obj.is_power_two(4);
		obj.is_power_two(7);
		obj.is_power_two(32);
		obj.is_power_two(63);
		obj.is_power_two(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
128 Is a power of two
/*
  Swift Program
  Check if given number is stable or unstable 
*/
class MyNumber
{
	//Detect that given number are part of power of 2 or not
	func is_power_two(_ number: Int)
	{
		var status: Bool = false;
		var counter: Int = 1;
		while (status == false && (2 << counter) <= number)
		{
			if ((2 << counter) == number)
			{
				//When result are exist in power of 2
				status = true;
			}
			counter += 1;
		}
		if (status == false)
		{
			print("", number ," Is not a power of two\n", terminator: "");
		}
		else
		{
			print("", number ," Is a power of two\n", terminator: "");
		}
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Cases
	obj.is_power_two(4);
	obj.is_power_two(7);
	obj.is_power_two(32);
	obj.is_power_two(63);
	obj.is_power_two(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
 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