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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Computer Science Books
- Watch Advanced C Programming Videos
- Buy C Books
- Apply for Computer Science Internship
- Apply for C Internship