From a6ccf31ad91076a7395d838775b8d01aa459e072 Mon Sep 17 00:00:00 2001 From: Nasser Afshin Date: Fri, 25 Oct 2019 02:38:46 +0330 Subject: [PATCH] Add a C program to check if a number is power of 2 This is writen as a one line-function of course. --- CONTRIBUTORS.md | 5 +++++ c/nasafix-nasser_isPowerOf2.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 c/nasafix-nasser_isPowerOf2.c diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c1b7b34b..cc3b6843 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -439,3 +439,8 @@ Name: [Antonio](https://github.com/toluwalope19)
Place: Nigeria
Coding Experience: Python and javaScript.
Email: tolubarca01@gmail.com
+ +Name: [Nasser](https://github.com/nasafix-nasser)
+Place: Iran
+Coding Experience: C/C++.
+Email: nader2003@yahoo.com
diff --git a/c/nasafix-nasser_isPowerOf2.c b/c/nasafix-nasser_isPowerOf2.c new file mode 100644 index 00000000..009d67a5 --- /dev/null +++ b/c/nasafix-nasser_isPowerOf2.c @@ -0,0 +1,18 @@ +#include +#include + +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; +}