Skip to content

Commit

Permalink
Add a C program to check if a number is power of 2
Browse files Browse the repository at this point in the history
This is writen as a one line-function of course.
  • Loading branch information
nasafix-nasser committed Oct 24, 2019
1 parent 8f98c5d commit a6ccf31
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,8 @@ Name: [Antonio](https://github.com/toluwalope19) </br>
Place: Nigeria </br>
Coding Experience: Python and javaScript. </br>
Email: [email protected] </br>

Name: [Nasser](https://github.com/nasafix-nasser) </br>
Place: Iran </br>
Coding Experience: C/C++. </br>
Email: [email protected] </br>
18 changes: 18 additions & 0 deletions c/nasafix-nasser_isPowerOf2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>

void is_powerof_2(int x)
{
for(int p = 1; p < x ; p *= 2, printf("%s%s", p == x ? "Yes\n" : "", p > x && x > 1 ? "No\n" : ""));
}

int main ( int argc, char *argv[] )
{
int value;
do {
printf("Enter a number > 1: ");
scanf("%d", &value);
}while(value <= 1);
is_powerof_2(value);
return EXIT_SUCCESS;
}

0 comments on commit a6ccf31

Please sign in to comment.