The power of a number says how many times to use the number in a multiplication. Powers are also called Exponents or Indices. For example, 8^2 could be called "8 to the power 2" or "8 to the second power", or simply "8 squared".

Some interesting fact about Power :
- If the indices is 1, then you just have the number itself. For example, 5^1 = 5
- If the indices is 0, then you get 1. For example, 5^0 = 1
- Exponents make it easier to write and use many multiplications
- Negative exponent means how many times to divide one by the number.For example, 5^-1 = 1 /5 = 0.2
How we check if a number is power of y for a given integer x ?
Naive solution:
Given two positive numbers x and y, check if y is a power of x or not.
Examples :
Input: x = 10, y = 1
Output: True
Input: x = 10, y = 1000
Output: True
Input: x = 10, y = 1001
Output: False
Approach : A simple solution is to repeatedly compute powers of x. If a power becomes equal to y, then y is a power, else not.
C++
// C++ program to check if a number is power of
// another number
#include <bits/stdc++.h>
using namespace std;
/* Returns 1 if y is a power of x */
bool isPower(int x, long int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
long int pow = 1;
while (pow < y)
pow *= x;
// Check if power of x becomes y
return (pow == y);
}
/* Driver program to test above function */
int main()
{
cout << (isPower(10, 1) ? "True" : "False") << endl;
cout << (isPower(1, 20) ? "True" : "False") << endl;
cout << (isPower(2, 128) ? "True" : "False") << endl;
cout << (isPower(2, 30) ? "True" : "False") << endl;
return 0;
}
Java
// Java program to check if a number is power of
// another number
public class Test {
// driver method to test power method
public static void main(String[] args)
{
// check the result for true/false and print.
System.out.println(isPower(10, 1) ? "True" : "False");
System.out.println(isPower(1, 20) ? "True" : "False");
System.out.println(isPower(2, 128) ? "True" : "False");
System.out.println(isPower(2, 30) ? "True" : "False");
}
/* Returns true if y is a power of x */
public static boolean isPower(int x, int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
int pow = 1;
while (pow < y)
pow = pow * x;
// Check if power of x becomes y
return (pow == y);
}
}
Python3
# Python program to check
# if a number is power of
# another number
# Returns true if y is a
# power of x
def isPower (x, y):
# The only power of 1
# is 1 itself
if (x == 1):
return (y == 1)
# Repeatedly compute
# power of x
pow = 1
while (pow < y):
pow = pow * x
# Check if power of x
# becomes y
return (pow == y)
# Driver Code
# check the result for
# true/false and print.
if(isPower(10, 1)): print("True")
else: print("False")
if(isPower(1, 20)): print("True")
else: print("False")
if(isPower(2, 128)): print("True")
else: print("False")
if(isPower(2, 30)): print("True")
else: print("False")
C#
// C# program to check if a number
// is power of another number
using System;
class GFG
{
// Returns true if y is a power of x
public static bool isPower (int x, int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly compute power of x
int pow = 1;
while (pow < y)
pow = pow * x;
// Check if power of x becomes y
return (pow == y);
}
// Driver Code
public static void Main ()
{
//check the result for true/false and print.
Console.WriteLine(isPower(10, 1) ? "True" : "False");
Console.WriteLine(isPower(1, 20) ? "True" : "False");
Console.WriteLine(isPower(2, 128) ? "True" : "False");
Console.WriteLine(isPower(2, 30) ? "True" : "False");
}
}
PHP
<?php
// PHP program to check if a
// number is power of another number
/* Returns 1 if y is a power of x */
function isPower($x, $y)
{
// The only power of 1 is 1 itself
if ($x == 1)
return ($y == 1 ? "True" : "False");
// Repeatedly comput power of x
$pow = 1;
while ($pow < $y)
$pow *= $x;
// Check if power of x becomes y
return ($pow == $y ? "True" : "False");
}
// Driver Code
echo isPower(10, 1) . "\n";
echo isPower(1, 20) . "\n";
echo isPower(2, 128) . "\n";
echo isPower(2, 30) . "\n";
?>
JavaScript
<script>
// Javascript program to check if a number is power of
// another number
/* Returns 1 if y is a power of x */
function isPower( x, y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);
// Repeatedly comput power of x
let pow = 1;
while (pow < y)
pow *= x;
// Check if power of x becomes y
return (pow == y);
}
// Driver Code
document.write((isPower(10, 1) ? "True" : "False") + "</br>");
document.write((isPower(1, 20) ? "True" : "False") + "</br>");
document.write((isPower(2, 128) ? "True" : "False") + "</br>");
document.write((isPower(2, 30) ? "True" : "False") + "</br>");
</script>
Output:
True
False
True
False
Time complexity of above solution is O(Logxy)
Auxiliary Space: O(1)
Basic Program related to Power :
More problems related to Powers :
Recent Articles on Power!
Similar Reads
Arithmetic Reasoning: Concepts, Questions & Solutions Arithmetic reasoning is an important skill that involves using principles of math to solve real-world practical problems and make informed decisions. The process of arithmetic reasoning includes analyzing numbers, identifying patterns, and drawing logical conclusions. This makes arithmetic reasoning
4 min read
Mathematics Practice Questions Mathematics Practice Questions: This collection of advanced maths practice questions is designed to test and enhance the problem-solving skills of students. These questions cover a wide range of mathematical topics, including algebra, geometry, and arithmetic operations, tailored for students seekin
9 min read
Expressions And Equations Expression and Equations are two important concepts of algebra in mathematics. We need to learn about expressions and equations to solve different types of easy and complex problems in both mathematics and real-life applications. expression is a combination of numbers, variables, and operators while
6 min read
Real World Math Problems Worksheets Although mathematics can appear abstract, applying mathematical ideas to practical problems makes studying more interesting and useful. Real-world math challenges enhance problem-solving abilities and help pupils see how mathematical ideas fit daily events. Real-world math problems are scenarios or
4 min read
How to Solve Arithmetic Word Problems? Arithmetic probably has the longest history during the time. It is a method of calculation that is been in use from ancient times for normal calculations like measurements, labeling, and all sorts of day-to-day calculations to obtain definite values. The term got originated from the Greek word "arit
5 min read
Equation in Maths | Definition , Types, Uses and Examples Equations in Maths are statements that equates two terms involving variables and constants. Equations are a form of statement that shows that the two expressions are equal. There are various types of equations - Linear, Quadratic, Polynomial, Logarithmic, Trigonometric as well as some other shape eq
15+ min read