Problem Description
Print the following pattern :
If N = 1
1
If N = 2
2 2 2
2 1 2
2 2 2
If N = 3
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
and so on.CODING ARENA
#include <stdio.h>
int main(){
int i, j, n;
int number_to_print;
scanf("%d", &n);
number_to_print = n;
for(i = 0; i < n; i++){
//--next_number_to_repeat;
number_to_print = n;
for(j =0 ; j< (2*n-1); j++){
printf("%d ", number_to_print);
if( j < i )
--number_to_print;
else if( j > ( (n*2)- i -3 ) )
++number_to_print;
}
printf("\n");
}
#if 1
for(i = n-2; i >= 0; i--){
number_to_print = n;
for(j =0 ; j < (2*n-1); j++){
printf("%d ", number_to_print);
if( j < i )
--number_to_print;
else if( j > ( (n*2)- i -3 ) )
++number_to_print;
}
printf("\n");
}
#endif
return 0;
}
Test Case 1
Input (stdin)3
Expected Output3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3Test Case 2
Input (stdin)2
Expected Output2 2 2
2 1 2
2 2 2
No comments:
Post a Comment