Print a pattern of numbers from 1 to n as shown below. Each of the numbers is separated by a single space.
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
Input Format
The input will contain a single integer n.
Constraints
1 <= n <= 1000
Sample Input 0
2
Sample Output 0
2 2 2
2 1 2
2 2 2
Sample Input 1
5
Sample Output 1
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
Sample Input 2
7
Sample Output 2
7 7 7 7 7 7 7 7 7 7 7 7 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 7 7 7 7 7 7 7 7 7 7 7 7
Printing Pattern using Loops - HackerRanker Solution
#include <stdio.h> int main() { int n; scanf("%d", & n); for (int i = n; i >= 1; i--) { int k = n; for (int sp = n; sp > i; sp--, k--) printf("%d ", k); for (int j = 1; j <= 2 * i - 1; j++) printf("%d ", i); int l = i + 1; for (int sp = n; sp > i; sp--, l++) printf("%d ", l); printf("\n"); } for (int i = 1; i < n; i++) { int k = n; for (int sp = 0; sp < n - i - 1; sp++, k--) printf("%d ", k); for (int j = 1; j <= 2 * (i + 1) - 1; j++) printf("%d ", i + 1); int m = i + 2; for (int sp = 0; sp < n - i - 1; sp++, m++) printf("%d ", m); printf("\n"); } }
No comments:
Post a Comment