打印沙漏数字图案的C语言程序
编写一个C语言程序,使用for循环打印沙漏数字模式。
#include <stdio.h
int main() { int i, j, k, rows;
printf("Enter Sandglass Number Pattern Rows = ");
scanf("%d",&rows);
printf("The Sandglass Number Pattern\\n");
for (i = 1 ; i <= rows; i++ )
{
for (j = 1; j < i; j++ )
{
printf(" ");
}
for (k = i; k <= rows; k++ )
{
printf("%d ", k);
}
printf("\\n");
}
for (i = rows - 1; i >= 1; i-- )
{
for (j = 1; j < i; j++ )
{
printf(" ");
}
for (k = i; k <= rows; k++ )
{
printf("%d ", k);
}
printf("\\n");
}
return 0;
这个C语言例子使用while循环打印沙漏图案中的数字。
#include <stdio.h>.
int main() { int i = 1, j, k, rows;
printf("Enter Sandglass Number Pattern Rows = ");
scanf("%d",&rows);
printf("\\nThe Sandglass Number Pattern\\n");
while( i <= rows)
{
j = 1;
while( j < i)
{
printf(" ");
j++;
}
k = i;
while( k <= rows )
{
printf("%d ", k);
k++;
}
printf("\\n");
i++;
}
i = rows - 1;
while( i >= 1)
{
j = 1;
while( j < i)
{
printf(" ");
j++;
}
k = i;
while( k <= rows )
{
printf("%d ", k);
k++;
}
printf("\\n");
i--;
}
return 0;
Enter Sandglass Number Pattern Rows = 7
The Sandglass Number Pattern
1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
4 5 6 7
5 6 7
6 7
7
6 7
5 6 7
4 5 6 7
3 4 5 6 7
2 3 4 5 6 7
1 2 3 4 5 6 7
使用do while循环显示沙漏数字模式的C程序。
#include <stdio.h>
int main() { int i = 1, j, k, rows;
printf("Enter Sandglass Number Pattern Rows = ");
scanf("%d",&rows);
printf("\\nThe Sandglass Number Pattern\\n");
do
{
j = 1;
do
{
printf(" ");
} while( j++ < i);
k = i;
do
{
printf("%d ", k);
} while( ++k <= rows );
printf("\\n");
} while( ++i <= rows);
i = rows - 1;
do
{
j = 1;
do
{
printf(" ");
} while( j++ < i);
k = i;
do
{
printf("%d ", k);
} while( ++k <= rows );
printf("\\n");
} while( --i >= 1);
return 0;
Enter Sandglass Number Pattern Rows = 9
The Sandglass Number Pattern
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9
8 9
7 8 9
6 7 8 9
5 6 7 8 9
4 5 6 7 8 9
3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9