C Program to Find whether a given integer is a power of three or not

In this blog post, I will teach you how to write C Program to check whether a given number is power of three. After reading this post you able to check whether given number is power of three or not.

But before writing the C program to how to check given number is power of 3 first let’s understand power of three.

An integer n is a power of three, if there exists an integer x such that n == 3x

For example,

Example 1:

Input: n = 27
Output: Yes
Explanation: 33 = 27

Example 2:

Input: n = 16
Output: NO

 

Let’s see the problem statements and constraints for this question.

Problem Statement: Given an integer n, return 1 if it is a power of three. Otherwise, return 0.

Constraints:

➤  -231 <= n <= 231 -1

 

Disclaimer: Try to solve the problem yourself first otherwise it would be not worth solving this problem.

 

Ways to check whether a given number is power of 3 or Not:

  • Using Logarithm (Math Approach).
  • Using while loop and if statements.
  • Recursive Approach.
  • Using Integer Limitations.
  • Using Integer Exponentiation.

Let’s discuss each of them to find whether given number is power of three or not.

 

C Program to check whether a given number is power of three or not:

Using Logarithm (Math Approach):

In the below C code, we check whether a given number num is a power of 3 or not using log function. This function returns 1 if number is power of 3 otherwise returns 0.

/**
 C Program to find whether a given number num
 is power of 3.
*/
#include <stdio.h>
#include<math.h>


int checkPowerOfThree(int num)
{
    int checkPowerOf3 = 0;

    /*0 and negative numbers
    are not powers of 3*/
    if(num ==1 )
    {
        checkPowerOf3 = 1;
    }
    else
    {
        if (num > 0)
        {
            // Calculate logarithm base x of 3
            double logarithm = (log(num) / log(3));

            // Check if the result is an integer
            checkPowerOf3 = ((logarithm - (int)logarithm) == 0);
        }
    }
    return checkPowerOf3;
}


int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    const int isPowerOfThree = checkPowerOfThree(num);
    if (isPowerOfThree != 0)
    {
        printf("%d is a power of three.\n", num);
    }
    else
    {
        printf("%d is not a power of three.\n", num);
    }
    return 0;
}

Output:

Enter a number: 81
81 is a power of three.

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(num) / log(3) is integer, then num is power of 3.

 

Using while loop:

It is one of simplest approach in which you need to divide num into 3. This means here, conceptualized as repeatedly subtracting num from until the remainder obtained is less than num. If the first time you get a non-zero remainder you know num is not an integer power of 3.

/**
 C Program to find whether a given number num
 is power of 3.
*/
#include <stdio.h>

int checkPowerOf3(int num)
{
    if(num>0)
    {
        while (num % 3 == 0)
        {
            num /= 3;
        }
    }
    // if num = 1 only then it is power of 3
    return (num == 1);
}


int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    const int isPowerOfTwo = checkPowerOf3(num);
    if (isPowerOfTwo != 0)
    {
        printf("%d is a power of three.\n", num);
    }
    else
    {
        printf("%d is not a power of three.\n", num);
    }
    return 0;
}

Output:

Enter a number: 81
81 is a power of three.

 

Recursive Approach:

In recursive approach we check whether given number is divisible of 3 or not. If the number is divisible by 3, keep checking the same for number/3 recursively. If the number reduced to 1, then the number is divisible by 3 else not.

/**
 C Program to find whether a given number num
 is power of 3.
*/
#include <stdio.h>


int checkPowerOf3(int num)
{
    if (num <= 0)
    {
        return 0;
    }
    //recursive call
    if ((num % 3) == 0)
    {
        return checkPowerOf3(num / 3);
    }
    if (num == 1)
    {
        return 1;
    }
    return 0;
}


int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    const int isPowerOfthree = checkPowerOf3(num);
    if (isPowerOfthree != 0)
    {
        printf("%d is a power of three.\n", num);
    }
    else
    {
        printf("%d is not a power of three.\n", num);
    }
    return 0;
}

Output:

Enter a number: 27
27 is a power of three.

 

Using Integer Limitations:

This approach using integer limitations to check if a number is a power of three or not. If integer size is 32bits, then the max integer number which divides from 3 is 1162261467(3^19).

/**
 C Program to find whether a given number num
 is power of 3.
*/
#include <stdio.h>


int checkPowerOf3(int num)
{
    return (num > 0)&&((1162261467 % num) == 0);
}


int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    const int isPowerOfthree = checkPowerOf3(num);
    if (isPowerOfthree != 0)
    {
        printf("%d is a power of three.\n", num);
    }
    else
    {
        printf("%d is not a power of three.\n", num);
    }
    return 0;
}

Output:

Enter a number: 81
81 is a power of three.

 

Using Integer Exponentiation:

This is simple approach to find whether a given number is power of 3 or not. In which we calculate the power of 3 using a multiplication till the multiplication is less than the given number. when calculated value is greater or equal to the given number then loop break. If calculated value is equal to number, then number is power of 3 otherwise not.

 

/**
 C Program to find whether a given number num
 is power of 3.
*/
#include <stdio.h>


int checkPowerOf3(int n)
{
    long long int pow = 1;
    while (pow < n)
    {
        pow *= 3;
    }
    /*
    If pow is equal to num,
      num is power of 3
    */
    return (pow == n);
}


int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    const int isPowerOfthree = checkPowerOf3(num);
    if (isPowerOfthree != 0)
    {
        printf("%d is a power of three.\n", num);
    }
    else
    {
        printf("%d is not a power of three.\n", num);
    }
    return 0;
}

 

 

Recommended Post:

 

Leave a Reply

Your email address will not be published. Required fields are marked *