Program to print pattern as shown in sample output.
Sample Input5
Sample Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Solution in | C | JAVA | PYTHON |
---|
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
for(int row=1;row<=n;row++){
for(int space=1;space<=n-row;space++){
printf(" ");
}
int k = 1;
for(int col=1;col<=row;col++){
printf("%d",k);
k = k*(row-col)/col;
if(col<=row-1){
printf(" ");
}
}
if(row<=n-1){
printf("\n");
}
}
return 0;
}
int main() {
int n;
scanf("%d",&n);
for(int row=1;row<=n;row++){
for(int space=1;space<=n-row;space++){
printf(" ");
}
int k = 1;
for(int col=1;col<=row;col++){
printf("%d",k);
k = k*(row-col)/col;
if(col<=row-1){
printf(" ");
}
}
if(row<=n-1){
printf("\n");
}
}
return 0;
}
Input
5
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
5
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1