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
C Program
#include <stdio.h>int main(){int n = 4;int p = n;n = 2 * n - 1;int x = n;int arr[101][101];int i, k = 0, l = 0;int m = n;while (k < m && l < n){for (i = l; i < n; i++){arr[k][i] = p;}k++;for (i = k; i < m; i++){arr[i][n - 1] = p;}n--;if (k < m){for (i = n - 1; i >= l; i--){arr[m - 1][i] = p;}m--;}if (l < n){for (i = m - 1; i >= k; i--){arr[i][l] = p;}l++;}p--;}for (int i = 0; i < x; i++){for (int j = 0; j < x; j++){printf("%d ", arr[i][j]);}printf("\n");}return 0;}
Input:
4
Output:
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
No comments:
Post a Comment