Smallest power of 2 greater than or equal to n

Here given code implementation process.

// C program for
// Smallest power of 2 greater than or equal to n
#include <stdio.h>

void findPower2(int n)
{
	// Start to 1
	int x = 1;
	while (x < n)
	{
		// Shift the value of x value by one to left side
		x = x << 1;
	}
	// Display calculated result
	printf("\n Given n : %d", n);
	printf("\n Result  : %d\n", x);
}
int main()
{
	// Test A
	int num = 17;
	findPower2(num);
	// Test B
	num = 64;
	findPower2(num);
	// Test C
	num = 15;
	findPower2(num);
	return 0;
}

Output

 Given n : 17
 Result  : 32

 Given n : 64
 Result  : 64

 Given n : 15
 Result  : 16
// Java program for
// Smallest power of 2 greater than or equal to n
public class Power
{
	public void findPower2(int n)
	{
		// Start to 1
		int x = 1;
		while (x < n)
		{
			// Shift the value of x value by one to left side
			x = x << 1;
		}
		// Display calculated result
		System.out.print("\n Given n : " + n);
		System.out.print("\n Result : " + x + "\n");
	}
	public static void main(String[] args)
	{
		Power task = new Power();
		// Test A
		int num = 17;
		task.findPower2(num);
		// Test B
		num = 64;
		task.findPower2(num);
		// Test C
		num = 15;
		task.findPower2(num);
	}
}

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Smallest power of 2 greater than or equal to n
class Power
{
	public: void findPower2(int n)
	{
		// Start to 1
		int x = 1;
		while (x < n)
		{
			// Shift the value of x value by one to left side
			x = x << 1;
		}
		// Display calculated result
		cout << "\n Given n : " << n;
		cout << "\n Result : " << x << "\n";
	}
};
int main()
{
	Power *task = new Power();
	// Test A
	int num = 17;
	task->findPower2(num);
	// Test B
	num = 64;
	task->findPower2(num);
	// Test C
	num = 15;
	task->findPower2(num);
	return 0;
}

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16
// Include namespace system
using System;
// Csharp program for
// Smallest power of 2 greater than or equal to n
public class Power
{
	public void findPower2(int n)
	{
		// Start to 1
		int x = 1;
		while (x < n)
		{
			// Shift the value of x value by one to left side
			x = x << 1;
		}
		// Display calculated result
		Console.Write("\n Given n : " + n);
		Console.Write("\n Result : " + x + "\n");
	}
	public static void Main(String[] args)
	{
		Power task = new Power();
		// Test A
		int num = 17;
		task.findPower2(num);
		// Test B
		num = 64;
		task.findPower2(num);
		// Test C
		num = 15;
		task.findPower2(num);
	}
}

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16
package main
import "fmt"
// Go program for
// Smallest power of 2 greater than or equal to n
type Power struct {}
func getPower() * Power {
	var me *Power = &Power {}
	return me
}
func(this Power) findPower2(n int) {
	// Start to 1
	var x int = 1
	for (x < n) {
		// Shift the value of x value by one to left side
		x = x << 1
	}
	// Display calculated result
	fmt.Print("\n Given n : ", n)
	fmt.Print("\n Result : ", x, "\n")
}
func main() {
	var task * Power = getPower()
	// Test A
	var num int = 17
	task.findPower2(num)
	// Test B
	num = 64
	task.findPower2(num)
	// Test C
	num = 15
	task.findPower2(num)
}

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16
<?php
// Php program for
// Smallest power of 2 greater than or equal to n
class Power
{
	public	function findPower2($n)
	{
		// Start to 1
		$x = 1;
		while ($x < $n)
		{
			// Shift the value of x value by one to left side
			$x = $x << 1;
		}
		// Display calculated result
		echo("\n Given n : ".$n);
		echo("\n Result : ".$x."\n");
	}
}

function main()
{
	$task = new Power();
	// Test A
	$num = 17;
	$task->findPower2($num);
	// Test B
	$num = 64;
	$task->findPower2($num);
	// Test C
	$num = 15;
	$task->findPower2($num);
}
main();

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16
// Node JS program for
// Smallest power of 2 greater than or equal to n
class Power
{
	findPower2(n)
	{
		// Start to 1
		var x = 1;
		while (x < n)
		{
			// Shift the value of x value by one to left side
			x = x << 1;
		}
		// Display calculated result
		process.stdout.write("\n Given n : " + n);
		process.stdout.write("\n Result : " + x + "\n");
	}
}

function main()
{
	var task = new Power();
	// Test A
	var num = 17;
	task.findPower2(num);
	// Test B
	num = 64;
	task.findPower2(num);
	// Test C
	num = 15;
	task.findPower2(num);
}
main();

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16
#  Python 3 program for
#  Smallest power of 2 greater than or equal to n
class Power :
	def findPower2(self, n) :
		#  Start to 1
		x = 1
		while (x < n) :
			#  Shift the value of x value by one to left side
			x = x << 1
		
		#  Display calculated result
		print("\n Given n : ", n, end = "")
		print("\n Result : ", x )
	

def main() :
	task = Power()
	#  Test A
	num = 17
	task.findPower2(num)
	#  Test B
	num = 64
	task.findPower2(num)
	#  Test C
	num = 15
	task.findPower2(num)

if __name__ == "__main__": main()

Output

 Given n :  17
 Result :  32

 Given n :  64
 Result :  64

 Given n :  15
 Result :  16
#  Ruby program for
#  Smallest power of 2 greater than or equal to n
class Power 
	def findPower2(n) 
		#  Start to 1
		x = 1
		while (x < n) 
			#  Shift the value of x value by one to left side
			x = x << 1
		end

		#  Display calculated result
		print("\n Given n : ", n)
		print("\n Result : ", x ,"\n")
	end

end

def main() 
	task = Power.new()
	#  Test A
	num = 17
	task.findPower2(num)
	#  Test B
	num = 64
	task.findPower2(num)
	#  Test C
	num = 15
	task.findPower2(num)
end

main()

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16
// Scala program for
// Smallest power of 2 greater than or equal to n
class Power()
{
	def findPower2(n: Int): Unit = {
		// Start to 1
		var x: Int = 1;
		while (x < n)
		{
			// Shift the value of x value by one to left side
			x = x << 1;
		}
		// Display calculated result
		print("\n Given n : " + n);
		print("\n Result : " + x + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Power = new Power();
		// Test A
		var num: Int = 17;
		task.findPower2(num);
		// Test B
		num = 64;
		task.findPower2(num);
		// Test C
		num = 15;
		task.findPower2(num);
	}
}

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16
// Swift 4 program for
// Smallest power of 2 greater than or equal to n
class Power
{
	func findPower2(_ n: Int)
	{
		// Start to 1
		var x: Int = 1;
		while (x < n)
		{
			// Shift the value of x value by one to left side
			x = x << 1;
		}
		// Display calculated result
		print("\n Given n : ", n, terminator: "");
		print("\n Result : ", x );
	}
}
func main()
{
	let task: Power = Power();
	// Test A
	var num: Int = 17;
	task.findPower2(num);
	// Test B
	num = 64;
	task.findPower2(num);
	// Test C
	num = 15;
	task.findPower2(num);
}
main();

Output

 Given n :  17
 Result :  32

 Given n :  64
 Result :  64

 Given n :  15
 Result :  16
// Kotlin program for
// Smallest power of 2 greater than or equal to n
class Power
{
	fun findPower2(n: Int): Unit
	{
		// Start to 1
		var x: Int = 1;
		while (x < n)
		{
			// Shift the value of x value by one to left side
			x = x shl 1;
		}
		// Display calculated result
		print("\n Given n : " + n);
		print("\n Result : " + x + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Power = Power();
	// Test A
	var num: Int = 17;
	task.findPower2(num);
	// Test B
	num = 64;
	task.findPower2(num);
	// Test C
	num = 15;
	task.findPower2(num);
}

Output

 Given n : 17
 Result : 32

 Given n : 64
 Result : 64

 Given n : 15
 Result : 16


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