In this blog post, I will teach you how to write Program to check whether a given integer is an integer power of another. That means a general solution to check if any number X is a power of Y or not, where Y is another integer number. After reading this post you able to check whether given integer number is power of another integer number or not.
An integer X is a power of Y, if there exists an integer p such that X == Yp.
Let’s see the problem statements and constraints for this question.
Problem Statement:
Given an integer X, return 1 if it is a power of Y. Otherwise, return 0.
Constraints:
➤ -231 <= n <= 231 -1
Ways to check whether a number X is a power of another number Y:
- Using Logarithm (Math Approach).
- Using while loop.
Let’s discuss each of them to find whether given number is power of three or not.
C program that checks if a number X is a power of another number Y:
Using Logarithm (Math Approach):
In the below C code, we find whether a given number X is a power of another number Y or not using log function. Below C function returns 1 if given number X is a power of another number Y otherwise returns 0.
/** C Program to find whether a given number x is power of y. */ #include <stdio.h> #include <math.h> int checkPowerOfY(int x, int y) { int checkPowerOfY = 0; if((x==1) && (y==1)) { checkPowerOfY = 1; } else { if ((x > 0) && (y > 0)) { const double denominator = 1.0; // Calculate logarithm base x of y double logarithm = (log(x) / log(y)); // Check if the result is an integer checkPowerOfY = (fmod(logarithm, denominator) == 0); } } return checkPowerOfY; } int main() { int x, y; printf("Enter the number X: "); scanf("%d", &x); printf("Enter the base number Y: "); scanf("%d", &y); if (checkPowerOfY(x, y)) { printf("%d is a power of %d.\n", x, y); } else { printf("%d is not a power of %d.\n", x, y); } return 0; }
Output:
Enter the number X: 32
Enter the base number Y: 2
32 is a power of 2.
Explanation:
In the above C code, we have put special handling for 1 and for other numbers that is greater than 0; we are calculating their logarithmic value. If result of expression log(x) / log(y) is integer, then x is power of y. We are verifying the integer with the help of fmod().
Note: You need to be careful of rounding issues with floating point numbers when comparing them.
Using while loop:
It is one of simplest approach in which you need to divide y into x. This means here, conceptualized as repeatedly subtracting y from x until the remainder obtained is less than y. If the first time you get a non-zero remainder you know x is not an integer power of y.
/** C Program to find whether a given number x is power of y. */ #include <stdio.h> int checkPowerOfY(int x, int y) { if((x>0) && (y > 0)) { while (x % y == 0) { x /= y; } } // if x = 1 only then it is power of y return (x == 1); } int main() { int x, y; printf("Enter the number X: "); scanf("%d", &x); printf("Enter the base number Y: "); scanf("%d", &y); if (checkPowerOfY(x, y)) { printf("%d is a power of %d.\n", x, y); } else { printf("%d is not a power of %d.\n", x, y); } return 0; }
Output:
Enter the number X: 32
Enter the base number Y: 2
32 is a power of 2.
Recommended Post:
- C program to calculate the power of a number.
- C Program to check whether a given number is power of two.
- Pow function in C.
- sqrt function in C.
- C program to find all roots of a quadratic equation using switch case.
- C program to find the roots of a quadratic equation.
- How to find whether a given number is prime number in C?
- C Program to swap two nibbles in a byte
- C Program to find first and last digit of a given number
- C program to find even and odd numbers
- C Program to find the sum of natural numbers up to n terms
- C Program to find the sum of even natural numbers from 1 to n