Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Advertisements