Printing Pattern Using Loops in C HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Printing Pattern Using Loops in C HackerRank Solution. This problem is a part of the HackerRank C Programming Series.

Printing Pattern Using Loops in C HackerRank Solution
Printing Pattern Using Loops in C HackerRank Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Printing Pattern Using Loops in C

Ezoicreport this adObjective

In this problem, you need to print the pattern of the following form containing the numbers from 1 to n.

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

Output Format

Print the pattern mentioned in the problem statement.

Example 0 :

Sample Input 0

2

Sample Output 0

2 2 2
2 1 2
2 2 2

Example 1 : 

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

Example 2 :

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 in C HackerRank Solution

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
    int i,j,k,m,n,x;
    scanf("%d",&n);
    k=n;
    m = n+(n-1);
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(i<=n-1)
            {
                if(i==0)
                {
                printf("%d ",k);
                }
                if(i>=1)
                {
                    if(j<i)
                    {
                        printf("%d ",k-j);
                    }
                    else if(j>=i && j<m-i)
                    {
                        printf("%d ",k-i);
                    }
                    else
                    {
                        printf("%d ",(j-k+1)+1);
                    }
                }
            }
            else if(i==n-1)
            {
                if(j<n)
                {
                    printf("%d ",k-j);
                }
                else
                {
                    printf("%d ",(j-k+1)+1);
                }
            }
            else if(i>=n)
            {
                x = m-i-1;
                if(i==m)
                {
                printf("%d ",k);
                }
                if(j<x)
                {
                    printf("%d ",k-j);
                }
                else if(j>=x && j<m-x)
                {
                    printf("%d ",k-x);
                }
                else
                {
                    printf("%d ",(j-k+1)+1);
                }
            }
        }
        printf("\n");
    }
}

Disclaimer: The above Problem (Printing Pattern Using Loops in C) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Variadic functions in C HackerRank Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad