c语言 47 显示出小于输入的整数的所有2的乘方

1、while语句

#include <stdio.h>

int main(void)
{
    int i = 2, j;
    puts("please input an integer.");
    do
    {
        printf("j = "); scanf("%d", &j);
        if (j <= 2)
            puts("the range of j is : > 2");
    }
    while (j <= 2);
    
    while (i <= j)
    {
        printf("%d ", i);
        i *= 2;
    }
    putchar('\n');
    return 0;
}

2、for语句

#include <stdio.h>

int main(void)
{
    int i, j;
    puts("please input an integer.");
    do
    {
        printf("j = "); scanf("%d", &j);
        if (j <= 2)
            puts("the range of j is : > 2.");
    }
    while (j <= 2);
    
    for (i = 2; i <= j; i *= 2)
    {
        printf("%d ", i);
    }
    putchar('\n');
    
    return 0;
}

3、do语句

#include <stdio.h>

int main(void)
{
    int i = 2, j;
    puts("please input an integer.");
    do
    {
        printf("j = "); scanf("%d", &j);
        if (j <= 2)
            puts("the range of j is : > 2 ");
    }
    while (j <= 2);
    
    do
    {
        printf("%d ", i);
        i *= 2;
    }
    while (i <= j);
    putchar('\n');
    
    return 0;
}
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14680597.html