
- 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 a power of another number in C++
Here we will see, whether a number is power of another number or not. Suppose a number 125, and another number 5 is given. So it will return true when it finds that 125 is power of 5. In this case it is true. 125 = 53.
Algorithm
isRepresentPower(x, y): Begin if x = 1, then if y = 1, return true, otherwise false pow := 1 while pow < y, do pow := pow * x done if pow = y, then return true return false End
Example
#include<iostream> #include<cmath> using namespace std; bool isRepresentPower(int x, int y) { if (x == 1) return (y == 1); long int pow = 1; while (pow < y) pow *= x; if(pow == y) return true; return false; } int main() { int x = 5, y = 125; cout << (isRepresentPower(x, y) ? "Can be represented" : "Cannot be represented"); }
Output
Can be represented
- Related Questions & Answers
- How to check if a number is a power of 2 in C#?
- Check if a number is power of 8 or not in C++
- Check if a number is divisible by all prime divisors of another number in C++
- Check if given number is a power of d where d is a power of 2 in Python
- Check if a number is power of k using base changing methods in C++
- Check if a number can be expressed as power in C++
- Check if a number is Palindrome in C++
- Check if a number is Bleak in C++
- Check if a number is a Krishnamurthy Number or not in C++
- Check if a number is a Mystery Numbers in C++
- Check if a number is a Trojan Numbers in C++
- Checking if a number is a valid power of 4 in JavaScript
- Check if a number is Full Prime in C++
- Check if a given number is Pronic in C++
- Check if a number is an Unusual Number or not in C++
Advertisements