如何将for循环转换为do while循环

时间:2017-03-05 09:27:09

标签: loops for-loop do-while

我可以寻求帮助吗?我需要使用do while循环而不是for循环。但是现在,我在如何将这些代码转换为do while循环方面有所不同。这是代码:

int main()
{
    int i, j, n;

    //Reads number of rows to be printed from user
    printf("Enter value of n : ");
    scanf("%d", &n);

    for(i=1; i<=n; i++)
    {
        for(j=i; j<=n; j++)
        {
            printf("*");
        }

        //Moves to the next line/row
        printf("\n");
    }

    return 0;
}

2 个答案:

答案 0 :(得分:0)

int main()
{


int i, j, n;

//Reads number of rows to be printed from user
printf("Enter value of n : ");
scanf("%d", &n);

i=1;
do
{
    j=i;
    do
    {
        printf("*");
        j++;


    }while(j<=n);
    printf("\n");
    i++;
}while(i<=n);

return 0;
}

答案 1 :(得分:0)

int main()
{
int i, j, n;

//Reads number of rows to be printed from user
printf("Enter value of n : ");
scanf("%d", &n);

i=1; 
do
{
 j=i;
    do
    {
        printf("*");
        j++;
    }while(j<=n);

    //Moves to the next line/row
    printf("\n");
    i++;
}while(i<=n);

return 0;
}