如何使用使用C的switch语句查找-14至14的数字平方

问题描述 投票:-3回答:1

使用switch语句或while循环,如何找到从-14到14的数字平方。我对应该如何开始感到困惑。

c loops while-loop switch-statement
1个回答
0
投票

只需从-14上升到14:

for(int i = -14; i <= 14; i++)
{
    printf("%d squared is %d\n", i, i * i);
}

while循环:

int i = -14;

while(i <= 14)
{
    printf("%d squared is %d\n", i, i * i);
    i++;
}

switch

switch(0)
{
    int i;
    default:
    i = -14;

    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    printf("%d squared is %d\n", i, i * i);
    i++;
    break;
}
© www.soinside.com 2019 - 2024. All rights reserved.