HackerRank Printing Pattern Using Loops problem solution in c

In this HackerRank Bitwise Operators problem in C programming, we need to make a program in c programming in which we need to 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


HackerRank Printing Pattern Using Loops problem solution in c


HackerRank Printing Pattern Using Loops problem solution in c

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

int n;
scanf("%d", &n);
int len = 2*n-1;
int min1,min2,min;

for (int i=1; i <= len; i++){
for ( int j=1; j<=len; j++){

min1 = i<=len-i ? i -1 : len-i;

min2 = j<=len-j ? j -1 : len-j;

min = min1<=min2 ? min1 : min2;

printf("%d ", n-min);
}
printf("\n");
}


return 0;
}



Post a Comment

1 Comments

  1. Please explain it. As a beginner its was difficult for me to understand

    ReplyDelete