c语言 4-18 输入一个整数值,显示该整数值个“*”,每显示5个就进行换行

 

1、while语句

#include <stdio.h>

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

 

3、do语句

#include <stdio.h>

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

 

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