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
Example 1:
Input:
1 |
32 |
Output:
1 |
32 is Power of 2 |
Example 2:
Input:
1 |
512 |
Output:
1 |
512 is Power of 2 |
Example 3:
Input:
1 |
15 |
Output:
1 |
15 is not Power of 2 |
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.
Program: Check Given Number is Power of 2 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include<stdio.h> int main() { int n, result=1; printf("Enter a Number : "); scanf("%d",&n); // Check if the given number is positive or not // If given number is negative number then Display error and return if(n<=0) { // Number is Negative, So display error message and usage info printf("%d is Negative Number, Please provide Positive Number \n", n); return 0; } // Calculate the largest power of 2 value, upto given number. // The loop will stop once 'result' reaches the 'n' // Loop also terminates if 'results' > 'n' while(result < n) { result = result * 2; } // If both 'result' and 'n' are Equal // Then The given number is Power of 2 // Otherwise Given number is not a power of 2 if(result == n) { printf("%d is Power of 2 \n",n); } else { printf("%d is not power of 2 \n",n); } return 0; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// Compiling the program using gcc compiler venkey@venkey$ gcc powerOf2.c // Run the Executable. venkey@venkey$ ./a.out Enter a Number : 32 32 is Power of 2 // Example 2 venkey@venkey$ ./a.out Enter a Number : 1024 1024 is Power of 2 // Example 3 venkey@venkey$ ./a.out Enter a Number : 4096 4096 is Power of 2 // Example 4 venkey@venkey$ ./a.out Enter a Number : 78 78 is not power of 2 // Example 5 venkey@venkey$ ./a.out Enter a Number : -100 -100 is Negative Number, Please provide Positive Number // Example 6 venkey@venkey$ ./a.out Enter a Number : 7196 7196 is not power of 2 venkey@venkey$ |
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.
Related Reading:
- Start Here – Step by step tutorials to Learn C programming Online with Thousand’s of Example Programs – SillyCodes
- Command line arguments in C programming with examples – SillyCodes
- Bit wise Operators in C Language – SillyCodes
3 Responses
[…] C Program to Check Number is Power of 2 […]
[…] C Program to Check Number is Power of 2 […]
[…] C Program to Check Number is Power of 2 […]