# C Program to check given number is power of 2 | Check Power of 2 program

## Program Introduction:

Write a C program to check if the given number is power of 2 or not (detect or check power of 2 program )

Here are couple examples with expected input and outputs

Input:

Output:

Input:

Output:

Input:

Output:

## Check Power of 2 program Description:

First of all, Take a variable â€˜ resultâ€™ and start to calculate all powers of 2 up to the given number and store the largest power of 2 numbers in the â€˜ resultâ€™ variable. Stop calculating the power of 2 once your â€˜ resultâ€™ variable reaches the given number.

Then check if the given number matches with our present â€˜ resultâ€™ variable value.

If the given number and â€˜ resultâ€™ variable value matches then the given number is Power of 2. Otherwise Given number is not a power of 2.

ðŸ“¢ The result variable default value should be 1, Instead of 0. Because 1 gives us the ability to calculate the power. If you take the default value as 0, Then you canâ€™t calculate the power because anything multiplied by zero is zero.

Also, Our program wonâ€™t take any Negative values. If user provides Negative number then we will display the error message. We are not checking the power of 2 for negative values in this program.

## Detect power of 2 Program Output:

We are using the GNU GCC compiler to compile our program. I am using the Ubuntu Linux operating system, So please use the IDE which ever you prefer.

Here is a quick tutorial to how to run programs in Linux â€“ Hello World Program in C language â€“ SillyCodes

## Conclusion:

In this article, We have discussed about how to calculate the power of 2 in c programming, Also discussed about checking the power of 2 by using the loops.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 3 Responses

1. [â€¦] C Program to Check Number is Power of 2 [â€¦]

2. [â€¦] C Program to Check Number is Power of 2 [â€¦]

3. [â€¦] C Program to Check Number is Power of 2 [â€¦]