反向印刷

时间:2011-06-06 02:53:09

标签: c for-loop

我正在制作一个打印一系列数字的循环程序。我想知道我如何反过来这样输出就像

123456789
12345678
1234567
123456
12345
1234
123
12
1

我的节目是

#include <stdio.h>
int main (void)
{
    //Local Declarations
    int limit;
    //Statements
    for (int lineCtrl = 1; lineCtrl <= 9; lineCtrl++)
    {
        for (int numCtrl = 1;
                 numCtrl <= lineCtrl;
                 numCtrl++)
        printf("%1d", numCtrl);
    printf("\n");
    }
    //to exit the program
    int temp;
    printf("Enter an integer and press Enter to exit the program: ");
    scanf("%d", &temp);
    return 0;
}

1 个答案:

答案 0 :(得分:2)

唯一更改的行是第一个用于循环初始化的行:

#include <stdio.h>
int main (void)
{
    //Local Declarations
    int limit;
    //Statements
    for (int lineCtrl = 9; lineCtrl >= 1; lineCtrl--)
    {
        for (int numCtrl = 1;
                 numCtrl <= lineCtrl;
                 numCtrl++)
        printf("%1d", numCtrl);
    printf("\n");
    }
    //to exit the program
    int temp;
    printf("Enter an integer and press Enter to exit the program: ");
    scanf("%d", &temp);
    return 0;
}