Program to print pattern as shown in sample output.
Sample Input4
Sample Output
1
3 2
4 5 6
10 9 8 7
Solution in | C | JAVA | PYTHON |
---|
#include<stdio.h>
int main() {
int n,row,col,i=1,j;
scanf("%d",&n);
for(row=1;row<=n;row++){
j = i+row-1;
for(col=1;col<=row;col++){
if(row%2!=0){
printf("%d",i++);
}else{
printf("%d",j--);
i++;
}
if(col<=row-1){
printf(" ");
}
}
if(row<=n-1){
printf("\n");
}
}
return 0;
}
int main() {
int n,row,col,i=1,j;
scanf("%d",&n);
for(row=1;row<=n;row++){
j = i+row-1;
for(col=1;col<=row;col++){
if(row%2!=0){
printf("%d",i++);
}else{
printf("%d",j--);
i++;
}
if(col<=row-1){
printf(" ");
}
}
if(row<=n-1){
printf("\n");
}
}
return 0;
}