如何在C中如下打印图案?

时间:2019-02-16 10:26:18

标签: c

#1234567
1#345678
23#56789
345#7890
4567#901
56789#12
678901#3
7890123#

这是我的代码

int main() {
    int pattern;
    int rows, columns;

    printf("Enter the pattern: ");
    scanf("%d", & pattern);

    for (rows = 1; rows <= pattern; rows++) {
        for (columns = 1; columns <= pattern; columns++) {
            if (rows == columns)
                printf("#");
            else
                printf("%d", columns);
        }
        printf("\n");
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

0到pattern-1 开始内循环和外循环。打印(columns+rows)%10。您的工作将会完成。请参见下面的代码更改:

int main() {
    int pattern;
    int rows, columns;

    printf("Enter the pattern: ");
    scanf("%d", & pattern);

    for (rows = 0; rows < pattern; rows++) {
        for (columns = 0; columns < pattern; columns++) {
            if (rows == columns)
                printf("#");
            else
                printf("%d", (columns+rows)%10);
        }
        printf("\n");
    }

    return 0;
}