c语言 4-7 显示出小于输入的整数的所有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;
}

 

posted @ 2021-04-20 13:06  小鲨鱼2018  阅读(140)  评论(0编辑  收藏  举报