Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
HackerRank Printing pattern using loop in c solution

HackerRank Printing Pattern using loops in c solution

YASH PAL, 16 July 202416 June 2025

HackerRank Printing pattern using loops in c problem solution – In this tutorial, we are going to solve HackerRank printing pattern using loop problems with practical program code examples and step-by-step solution. in this problem we need to print a pattern of numbers from 1 to n. we need to separate each number by a single space. we need to take a input from user that will be greater than or equal to 1 and less than or equal to 1000.

Video explanation

Printing pattern using loop

Using this problem we are going to learn about how to print patterns using the for loop in c programming.

HackerRank Printing pattern using loop in c solution
HackerRank Printing pattern using loop in c solution

HackerRank Printing Pattern using loops C Solution.

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

int main() 
{

    int n;
    scanf("%d", &n);
    int len = n*2 - 1;
    for(int i=0;i<len;i++){
        for(int j=0;j<len;j++){
            int min = i < j ? i : j;
            min = min < len-i ? min : len-i-1;
            min = min < len-j-1 ? min : len-j-1;
            printf("%d ", n-min);
        }
        printf("\n");
    }
    return 0;
}

Second solution

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

int main() {

int n;
scanf("%d", &n);
int len = 2*n-1;
int min1,min2,min;
  // Complete the code to print the pattern.
  // for rows
  for (int i=1; i <=len; i++) {
      // for col
      for (int j=1; j<=len; j++) {
        // min diff btn vertical sides
        min1 = i<=len-i ? i -1 : len-i;
        // min diff btn horizontal sides
        min2 = j<=len-j ? j -1: len-j;
        // min diff btn vertical & horizontal sides
        min = min1<=min2 ? min1 : min2;
        // print  
        printf("%d ",n-min);
      }
      printf("\n");
  }
return 0;
}

Next problem solution – HackerRank 1D Arrays in C solution

C Solutions coding problems solutions Hackerrank Problems Solutions cHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes