以下是一个用C语言实现输出字符菱形的示例程序:
#includeint main() { int n, c, k, space = 1; printf("Enter number of rows: "); scanf("%d", &n); space = n - 1; for (k = 1; k <= n; k++) { for (c = 1; c <= space; c++) printf(" "); space--; for (c = 1; c <= 2*k-1; c++) printf("*"); printf("\n"); } space = 1; for (k = 1; k <= n - 1; k++) { for (c = 1; c <= space; c++) printf(" "); space++; for (c = 1 ; c <= 2*(n-k)-1; c++) printf("*"); printf("\n"); } return 0; }
该程序会要求用户输入行数,然后输出一个以星号(*)组成的菱形。如果想输出其他字符菱形,只需将 printf("*")
替换为 printf("你想要的字符")
即可。