- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find whether a given number is a power of 4 or not in C++
In this problem, we are given an integer N. Our task is to find whether a given integer is a power of 4 or not.
Let's take an example to understand the problem,
Input : N = 64 Output : Yes
Explanation −
43 = 64
Solution Approach
A simple solution to the problem is by recursively dividing the number by 4 and checking whether the resultant number is divided by 4 or not. If the value after recursive division becomes 1, return true.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; bool isPowerOf4(int n){ if(n == 0) return 0; while(n != 1) { if(n % 4 != 0) return 0; n = n / 4; } return 1; } int main(){ int n = 123454; if (isPowerOf4(n)) cout<<"The number is a power of 4"; else cout<<"The number is not a power of 4"; return 0; }
Output
The number is not a power of 4
- Related Articles
- Find whether a given integer is a power of 3 or not in C++
- Check if a number is power of 8 or not in C++
- Program to check whether the given number is Buzz Number or not in C++
- C++ Program to find whether a number is the power of two?
- Write a Golang program to check whether a given number is prime number or not
- Golang Program to check whether given positive number is power of 2 or not, without using any branching or loop
- Program to check whether a number is Proth number or not in C++
- Check whether the given number is Euclid Number or not in Python
- Write a Golang program to check whether a given number is a palindrome or not
- Find whether a subarray is in form of a mountain or not in C++
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
- Check whether a number is a Fibonacci number or not JavaScript
- C# program to check whether a given string is Heterogram or not

Advertisements