Check if a number is power of 8 or not
Here given code implementation process.
// C Program
// Check if a number is power of 8 or not
#include <stdio.h>
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
void powerof8(int num)
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if ((num && !(num & 3067833782) && !(num & (num - 1))) == 1)
{
printf(" %d is power of 8\n", num);
}
else
{
printf(" %d is not power of 8\n", num);
}
}
int main(int argc, char
const *argv[])
{
// Test Case
powerof8(8);
powerof8(64);
powerof8(128);
powerof8(512);
powerof8(32);
powerof8(4096);
return 0;
}
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
/*
Java Program
Check if a number is power of 8 or not
*/
public class Power
{
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
public void powerof8(int num)
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if ( num != 0 && ((num & 3067833782L) == 0) && ((num & (num -1)) == 0) )
{
System.out.print(" " + num + " is power of 8\n");
}
else
{
System.out.print(" " + num + " is not power of 8\n");
}
}
public static void main(String[] args)
{
Power task = new Power();
// Test Case
task.powerof8(8);
task.powerof8(64);
task.powerof8(128);
task.powerof8(512);
task.powerof8(32);
task.powerof8(4096);
}
}
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Check if a number is power of 8 or not
*/
class Power
{
public:
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
void powerof8(int num)
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if (num != 0 && ((num &3067833782) == 0) && ((num &(num - 1)) == 0))
{
cout << " " << num << " is power of 8\n";
}
else
{
cout << " " << num << " is not power of 8\n";
}
}
};
int main()
{
Power task = Power();
// Test Case
task.powerof8(8);
task.powerof8(64);
task.powerof8(128);
task.powerof8(512);
task.powerof8(32);
task.powerof8(4096);
return 0;
}
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
// Include namespace system
using System;
/*
C# Program
Check if a number is power of 8 or not
*/
public class Power
{
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
public void powerof8(int num)
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if (num != 0 && ((num & 3067833782) == 0) && ((num & (num - 1)) == 0))
{
Console.Write(" " + num + " is power of 8\n");
}
else
{
Console.Write(" " + num + " is not power of 8\n");
}
}
public static void Main(String[] args)
{
Power task = new Power();
// Test Case
task.powerof8(8);
task.powerof8(64);
task.powerof8(128);
task.powerof8(512);
task.powerof8(32);
task.powerof8(4096);
}
}
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
<?php
/*
Php Program
Check if a number is power of 8 or not
*/
class Power
{
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
public function powerof8($num)
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if ($num != 0 && (($num & 3067833782) == 0) && (($num & ($num - 1)) == 0))
{
echo " ". $num ." is power of 8\n";
}
else
{
echo " ". $num ." is not power of 8\n";
}
}
}
function main()
{
$task = new Power();
// Test Case
$task->powerof8(8);
$task->powerof8(64);
$task->powerof8(128);
$task->powerof8(512);
$task->powerof8(32);
$task->powerof8(4096);
}
main();
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
/*
Node Js Program
Check if a number is power of 8 or not
*/
class Power
{
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
powerof8(num)
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if (num != 0 && ((num & 3067833782) == 0) && ((num & (num - 1)) == 0))
{
process.stdout.write(" " + num + " is power of 8\n");
}
else
{
process.stdout.write(" " + num + " is not power of 8\n");
}
}
}
function main()
{
var task = new Power();
// Test Case
task.powerof8(8);
task.powerof8(64);
task.powerof8(128);
task.powerof8(512);
task.powerof8(32);
task.powerof8(4096);
}
main();
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
# Python 3 Program
# Check if a number is power of 8 or not
class Power :
# Determine whether given number is power of 8 or not
# 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
def powerof8(self, num) :
# 3067833782 (32 bit) = 10110110110110110110110110110110
# position % 3 bit are inactive
if (num != 0 and((num & 3067833782) == 0) and((num & (num - 1)) == 0)) :
print("", num ,"is power of 8")
else :
print("", num ,"is not power of 8")
def main() :
task = Power()
# Test Case
task.powerof8(8)
task.powerof8(64)
task.powerof8(128)
task.powerof8(512)
task.powerof8(32)
task.powerof8(4096)
if __name__ == "__main__": main()
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
# Ruby Program
# Check if a number is power of 8 or not
class Power
# Determine whether given number is power of 8 or not
# 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
def powerof8(num)
# 3067833782 (32 bit) = 10110110110110110110110110110110
# position % 3 bit are inactive
if (num != 0 && ((num & 3067833782) == 0) && ((num & (num - 1)) == 0))
print(" ", num ," is power of 8\n")
else
print(" ", num ," is not power of 8\n")
end
end
end
def main()
task = Power.new()
# Test Case
task.powerof8(8)
task.powerof8(64)
task.powerof8(128)
task.powerof8(512)
task.powerof8(32)
task.powerof8(4096)
end
main()
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
/*
Scala Program
Check if a number is power of 8 or not
*/
class Power
{
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
def powerof8(num: Int): Unit = {
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if (num != 0 && ((num & 3067833782L) == 0) && ((num & (num - 1)) == 0))
{
print(" " + num + " is power of 8\n");
}
else
{
print(" " + num + " is not power of 8\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Power = new Power();
// Test Case
task.powerof8(8);
task.powerof8(64);
task.powerof8(128);
task.powerof8(512);
task.powerof8(32);
task.powerof8(4096);
}
}
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
/*
Swift 4 Program
Check if a number is power of 8 or not
*/
class Power
{
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
func powerof8(_ num: Int)
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if (num != 0 && ((num & 3067833782) == 0) && ((num & (num - 1)) == 0))
{
print(" ", num ," is power of 8");
}
else
{
print(" ", num ," is not power of 8");
}
}
}
func main()
{
let task: Power = Power();
// Test Case
task.powerof8(8);
task.powerof8(64);
task.powerof8(128);
task.powerof8(512);
task.powerof8(32);
task.powerof8(4096);
}
main();
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
/*
Kotlin Program
Check if a number is power of 8 or not
*/
class Power
{
// Determine whether given number is power of 8 or not
// 8¹,8²,8³,8⁴,8⁵,8⁶,8⁷,8⁸,8⁹ ...
fun powerof8(num: Int): Unit
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if (num != 0 && ((num.toLong() and 3067833782L).toInt() == 0) && ((num and(num - 1)) == 0))
{
print(" " + num + " is power of 8\n");
}
else
{
print(" " + num + " is not power of 8\n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Power = Power();
// Test Case
task.powerof8(8);
task.powerof8(64);
task.powerof8(128);
task.powerof8(512);
task.powerof8(32);
task.powerof8(4096);
}
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
// Rust Program
// Check if a number is power of 8 or not
fn main()
{
// Test Case
powerof8(8);
powerof8(64);
powerof8(128);
powerof8(512);
powerof8(32);
powerof8(4096);
}
fn powerof8(num: i64)
{
// 3067833782 (32 bit) = 10110110110110110110110110110110
// position % 3 bit are inactive
if num != 0 && ((num & 3067833782) == 0) && ((num & (num -1)) == 0)
{
print!(" {} is power of 8\n", num);
}
else
{
print!(" {} is not power of 8\n", num);
}
}
Output
8 is power of 8
64 is power of 8
128 is not power of 8
512 is power of 8
32 is not power of 8
4096 is power of 8
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