

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a number is power of 8 or not in C++
In this section, we will see, if a number is the power of 8 or not using some easier method. If a number like 4096 is there, then the program will return true, as this is the power of 8.
The trick is simple. we will calculate log8(num). If this is an integer, then n is the power of 8. Here we will use the tranc(n) function to find the closest integer of the double value.
Example
#include <iostream> #include <cmath> using namespace std; bool isPowerOfEight(int n) { double val = log(n)/log(8); //get log n to the base 8 return (val - trunc(val) < 0.000001); } int main() { int number = 4096; if(isPowerOfEight(number)){ cout << number << " is power of 8"; } else { cout << number << " is not power of 8"; } }
Output
4096 is power of 8
- Related Questions & Answers
- Check if a large number is divisible by 8 or not in C++
- Program to check a number is power of two or not in Python
- Check if a number is jumbled or not in C++
- Check if a number is a Krishnamurthy Number or not in C++
- Check if a number is a power of another number in C++
- Check if a number is an Unusual Number or not in C++
- Check if a number is an Achilles number or not in C++
- Check if a number is an Achilles number or not in Python
- Check if a number is Quartan Prime or not in C++
- Check if a given number is sparse or not in C++
- Check if a number is Primorial Prime or not in C++
- Check if a number is Primorial Prime or not in Python
- Check if given number is Emirp Number or not in Python
- Check if a number is a Pythagorean Prime or not in C++
- C# Program to check if a number is prime or not
Advertisements