/* Include lib */
#include <stdio.h>
#include <assert.h>
/* Define */
/* API calc power of 2 */
/* Calc 2^k with k is natural number (positive integer, except 0) */
int power_of_2(int k, int *ret)
{
int i, tmp = 1;
if (k <= 0)
return -1;
for (i = 0; i < k; i++)
tmp *= 2;
*ret = tmp;
return 0;
}
/* Main func interract with user */
int main(void)
{
int sel, k, ret;
while (1) {
printf("\n\nSelection:\n");
printf("0: input k for calculating 2^k\n");
printf("1: Exit\n");
printf("Select: ");
scanf("%d", &sel);
if (sel == 0) {
printf("Input k: ");
scanf("%d", &k);
if (!power_of_2(k, &ret)) {
printf("=> %d\n", ret);
} else {
printf("Input must be natural (integer > 0)\n");
}
} else {
return 0;
}
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: