

- 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 can be expressed as x^y (x raised to power y) in C++
Here we will check whether we can represent a number as power, like xy or not. Suppose a number 125 is present. This can be represented as 53. Another number 91 cannot be represented as power of some integer value.
Algorithm
isRepresentPower(num): Begin if num = 1, then return true for i := 2, i2 <= num, increase i by 1, do val := log(a)/log(i) if val – int(val) < 0.0000000001, then return true done return false End
Example
#include<iostream> #include<cmath> using namespace std; bool isRepresentPower(int num) { if (num == 1) return true; for (int i = 2; i * i <= num; i++) { double val = log(num) / log(i); if ((val - (int)val) < 0.00000001) return true; } return false; } int main() { int n = 125; cout << (isRepresentPower(n) ? "Can be represented" : "Cannot be represented"); }
Output
Can be represented
- Related Questions & Answers
- Check if a number can be expressed as 2^x + 2^y in C++
- Check if a number can be expressed as power in C++
- Find value of y mod (2 raised to power x) in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Check if a number can be expressed as a^b in C++
- Check if a number can be expressed as a^b in Python
- Find larger of x^y and y^x in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Check if a number can be expressed as sum two abundant numbers in C++
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Count of pairs (x, y) in an array such that x < y in C++
- How to plot y=1/x as a single graph in Python?
Advertisements