Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a C program to check if a number is power of 2 #416

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,8 @@ Name: [Abhishek Singh](https://github.com/n6wbi6) </br>
Place: India </br>
Coding Experience: 2 years of coding experience in C and python. </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;
}