c语言 4-7 编写一段程序,显示小于输入的整数的所有2的乘方。

 

1、do语句

#include <stdio.h>

#include <math.h>

int main(void)
{
    int i = 1, j;
    puts("please input an integer.");
    printf("j = "); scanf("%d", &j);
    
    do
    {
        if (pow (2, i) < j)
            printf("%.f ", pow (2, i));
        i++;
    }
    while (pow (2, i) < j);
    putchar('\n');
    return 0;
}

 

2、while语句

#include <stdio.h>

#include <math.h>

int main(void)
{
    int i = 1, j;
    puts("please input an integer.");
    printf("j = "); scanf("%d", &j);
    
    while (pow(2, i) < j)
    {
        printf("%.f ", pow(2, i));
        i++;
    }
    putchar('\n');
    
    return 0;
}

 

3、for语句

#include <stdio.h>

#include <math.h>

int main(void)
{
    int i = 1, j;
    puts("please input an integer.");
    printf("j = "); scanf("%d", &j);
    
    for(pow(2,i); pow(2,i) < j; i++)
    {
        printf("%.f ", pow(2,i));
    }
    putchar('\n');
    
    return 0;
}

 

posted @ 2021-04-19 10:29  小鲨鱼2018  阅读(356)  评论(0编辑  收藏  举报