c语言417 显示1到n的整数值的二次方

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)
    {
        printf("the square of %d is %d.\n", i, i * i);
        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++)
    {
        printf("the square of %d is %d.\n", i, i * i);
    }
    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
    {
        printf("the square of %d is %d.\n", i, i * i);
        i++;
    }
    while (i <= j);
    
    return 0;
}
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14682401.html