This C Program checks if a given integer is a power of 2 without using bitwise.
Here is source code of the C Program to check if a given integer is a power of 2 without using bitwise. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Check if a given Integer is a Power of 2 without using Bitwise
*/
#include <stdio.h>
/* function prototype */
int power_of_2(unsigned int);
/* gloabal variables */
int b[32] = {0}, j = 0, n, i, count = 0;
void main()
{
unsigned int num;
printf("enter value\n");
scanf("%d", &num);
power_of_2(num);
if (count == 1)
printf("YES\n");
else
printf("NO\n");
}
/* function to check whether a given number is power of 2 or not */
int power_of_2(unsigned int num)
{
while (num != 0)
{
n = num % 2;
if (n == 1)
count++;
num = num / 2;
}
}
$ cc bit26.c $ a.out enter value 128 YES $ a.out enter value 126 NO
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for C Internship
- Practice BCA MCQs
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check Computer Science Books