Topic Collection of Zhejiang University Edition "C Language Programming (3rd Edition)"
Exercise 5-3 Number Pyramid (15 points)
This question requires the realization of the function output n-line digital pyramid.
Function interface definition:
void pyramid( int n );
Among them n
is the parameter passed in by the user, which is a positive integer of [1, 9]. Print out the required functions in accordance with the format of the sample as shown in n
the row number of the pyramid. Note that
each number is followed by a space.
Sample referee test procedure:
#include <stdio.h>
void pyramid( int n );
int main()
{
int n;
scanf("%d", &n);
pyramid(n);
return 0;
}
/* 你的代码将被嵌在这里 */
Input sample:
5
Sample output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Code:
void pyramid(int n){
int i,j,k;
for(i=1;i<=n;i++){
for(j=1;j<=n-i;j++){
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%d ",i);
}
printf("\n");
}
}