Printing Pattern using Loops - Hackerrank solution

 

Printing Pattern Using Loops


Task


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


Solution:

#include <stdio.h>

int main()
{
    int n, i, j, t; 

    scanf("%d", &n);

    t = 2 * n - 1;
    i = t; 
    j = t;
    int a[i][j], k, m, p;
    p = n;
    m = 0;

    for (k = 0; k < p; k++) {
        for (i = m; i < t; i++) {
            for (j = m; j < t; j++) {
                if (i == m || i == (t - 1) || j == m || j == (t - 1)) {
                    a[i][j] = n;
                    if (n == 1) {
                        break;
                    }
                }
            }
        }
        t = t - 1;
        n = n - 1;
        m = m + 1;
    }
    t = 2 * m - 1;
    for (i = 0; i < t; i++) {
        for (j = 0; j < t; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

    return 0;
}



Note:

This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.

Disclaimer:-

 The above all problem statement is given by hackerrank.com, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You. 

Printing Pattern using Loops - Hackerrank solution Printing Pattern using Loops - Hackerrank solution Reviewed by CodexRitik on March 15, 2020 Rating: 5

4 comments:

  1. Replies
    1. Printing for Pattern You should have a knowledge of loops,because here we are using three loops for handling a space,row and column.

      Delete
  2. Amazing! whatever you mention here that all information are useful! महात्मा गाँधी निबंध

    ReplyDelete
  3. You can either take previously existing stencils or you can plan your own; you can even acknowledge clients' plans and concocted one of a kind things. https://once-tech.com/bottle-screen-printing-machines/

    ReplyDelete

Powered by Blogger.