Highest power of two that divides a given number

Here given code implementation process.

// C program 
// Highest power of two that divides a given number
#include <stdio.h>

// Find higher power 2 number which is divisible by given number
void power2Divisible(int num)
{
	// Find highest divide number
	int n = (num & (~(num - 1)));
	// Display calculated result
	printf("\n Number : %d", num);
	printf("\n Power  : %d\n", n);
}
int main(int argc, char
	const *argv[])
{
	// Test Cases
	power2Divisible(10);
	power2Divisible(40);
	power2Divisible(64);
	power2Divisible(43);
	return 0;
}

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1
/*
  Java program
  Highest power of two that divides a given number
*/
public class Power
{
	// Find higher power 2 number which is divisible by given number
	public void power2Divisible(int num)
	{
		// Find highest divide number
		int n = (num & (~(num - 1)));
		// Display calculated result
		System.out.print("\n Number : " + num);
		System.out.print("\n Power  : " + n + "\n");
	}
	public static void main(String[] args)
	{
		Power task = new Power();
		// Test Cases
		task.power2Divisible(10);
		task.power2Divisible(40);
		task.power2Divisible(64);
		task.power2Divisible(43);
	}
}

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1
// Include header file
#include <iostream>

using namespace std;
/*
  C++ program
  Highest power of two that divides a given number
*/
class Power
{
	public:
		// Find higher power 2 number which is divisible by given number
		void power2Divisible(int num)
		{
			// Find highest divide number
			int n = (num &(~(num - 1)));
			// Display calculated result
			cout << "\n Number : " << num;
			cout << "\n Power  : " << n << "\n";
		}
};
int main()
{
	Power task = Power();
	// Test Cases
	task.power2Divisible(10);
	task.power2Divisible(40);
	task.power2Divisible(64);
	task.power2Divisible(43);
	return 0;
}

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1
// Include namespace system
using System;
/*
  C# program
  Highest power of two that divides a given number
*/
public class Power
{
	// Find higher power 2 number which is divisible by given number
	public void power2Divisible(int num)
	{
		// Find highest divide number
		int n = (num & (~(num - 1)));
		// Display calculated result
		Console.Write("\n Number : " + num);
		Console.Write("\n Power  : " + n + "\n");
	}
	public static void Main(String[] args)
	{
		Power task = new Power();
		// Test Cases
		task.power2Divisible(10);
		task.power2Divisible(40);
		task.power2Divisible(64);
		task.power2Divisible(43);
	}
}

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1
<?php
/*
  Php program
  Highest power of two that divides a given number
*/
class Power
{
	// Find higher power 2 number which is divisible by given number
	public	function power2Divisible($num)
	{
		// Find highest divide number
		$n = ($num & (~($num - 1)));
		// Display calculated result
		echo "\n Number : ". $num;
		echo "\n Power  : ". $n ."\n";
	}
}

function main()
{
	$task = new Power();
	// Test Cases
	$task->power2Divisible(10);
	$task->power2Divisible(40);
	$task->power2Divisible(64);
	$task->power2Divisible(43);
}
main();

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1
/*
  Node Js program
  Highest power of two that divides a given number
*/
class Power
{
	// Find higher power 2 number which is divisible by given number
	power2Divisible(num)
	{
		// Find highest divide number
		var n = (num & (~(num - 1)));
		// Display calculated result
		process.stdout.write("\n Number : " + num);
		process.stdout.write("\n Power  : " + n + "\n");
	}
}

function main()
{
	var task = new Power();
	// Test Cases
	task.power2Divisible(10);
	task.power2Divisible(40);
	task.power2Divisible(64);
	task.power2Divisible(43);
}
main();

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1
#   Python 3 program
#   Highest power of two that divides a given number

class Power :
	#  Find higher power 2 number which is divisible by given number
	def power2Divisible(self, num) :
		#  Find highest divide number
		n = (num & (~(num - 1)))
		#  Display calculated result
		print("\n Number : ", num, end = "")
		print("\n Power  : ", n )
	

def main() :
	task = Power()
	#  Test Cases
	task.power2Divisible(10)
	task.power2Divisible(40)
	task.power2Divisible(64)
	task.power2Divisible(43)

if __name__ == "__main__": main()

Output

 Number :  10
 Power  :  2

 Number :  40
 Power  :  8

 Number :  64
 Power  :  64

 Number :  43
 Power  :  1
#   Ruby program
#   Highest power of two that divides a given number

class Power 
	#  Find higher power 2 number which is divisible by given number
	def power2Divisible(num) 
		#  Find highest divide number
		n = (num & (~(num - 1)))
		#  Display calculated result
		print("\n Number : ", num)
		print("\n Power  : ", n ,"\n")
	end

end

def main() 
	task = Power.new()
	#  Test Cases
	task.power2Divisible(10)
	task.power2Divisible(40)
	task.power2Divisible(64)
	task.power2Divisible(43)
end

main()

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1
/*
  Scala program
  Highest power of two that divides a given number
*/
class Power
{
	// Find higher power 2 number which is divisible by given number
	def power2Divisible(num: Int): Unit = {
		// Find highest divide number
		var n: Int = (num & (~(num - 1)));
		// Display calculated result
		print("\n Number : " + num);
		print("\n Power  : " + n + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Power = new Power();
		// Test Cases
		task.power2Divisible(10);
		task.power2Divisible(40);
		task.power2Divisible(64);
		task.power2Divisible(43);
	}
}

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1
/*
  Swift 4 program
  Highest power of two that divides a given number
*/
class Power
{
	// Find higher power 2 number which is divisible by given number
	func power2Divisible(_ num: Int)
	{
		// Find highest divide number
		let n: Int = (num & (~(num - 1)));
		// Display calculated result
		print("\n Number : ", num, terminator: "");
		print("\n Power  : ", n );
	}
}
func main()
{
	let task: Power = Power();
	// Test Cases
	task.power2Divisible(10);
	task.power2Divisible(40);
	task.power2Divisible(64);
	task.power2Divisible(43);
}
main();

Output

 Number :  10
 Power  :  2

 Number :  40
 Power  :  8

 Number :  64
 Power  :  64

 Number :  43
 Power  :  1
/*
  Kotlin program
  Highest power of two that divides a given number
*/
class Power
{
	// Find higher power 2 number which is divisible by given number
	fun power2Divisible(num: Int): Unit
	{
		// Find highest divide number
		var n: Int = (num and((num - 1).inv()));
		// Display calculated result
		print("\n Number : " + num);
		print("\n Power  : " + n + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Power = Power();
	// Test Cases
	task.power2Divisible(10);
	task.power2Divisible(40);
	task.power2Divisible(64);
	task.power2Divisible(43);
}

Output

 Number : 10
 Power  : 2

 Number : 40
 Power  : 8

 Number : 64
 Power  : 64

 Number : 43
 Power  : 1

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