Table of contents
1.
Introduction
1.1.
Pattern
2.
Approach
2.1.
Code using For Loop
2.2.
Code using While Loop
2.3.
Complexity Analysis
3.
Frequently Asked Questions
3.1.
How can we make a half pyramid number pattern using C?
3.2.
Which looping statements are used to print a half pyramid pattern?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Half Pyramid Number Pattern

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

Introduction

The pattern codes serve as crucial interview questions in many companies to date. Pyramid patterns are one of those important questions. In this tutorial, we will discuss the concept of C programming language using which we will print half pyramid number patterns. We will take user input through the scanf( ) statement and print the pattern accordingly.

Pattern

For Number of Rows = 5, the pattern is as follows
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Approach

From the above pattern, it is clear, in the first row, we print 1 once, for the 2nd row, we print 2 twice, for the 3rd row, we print 3 thrice and so on. We can achieve the following pattern using nested loops. One loop to maintain the row number and the second loop to print the number multiple times according to the condition.

Code using For Loop

#include<stdio.h>
void main(){
int n=6,i,j;
for(i=1;i<=n;i++){
     for(j=1;j<=i;j++){
         printf("%d ",i);
     }
     printf("\n");
}
}
You can also try this code with Online C Compiler
Run Code

Output

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6

Code using While Loop

#include<stdio.h>
void main(){
int n=5,i,j;
i=1;
while(i<=n){
     j=1;
     while(j<=i){
         printf("%d ",i);
        j++;
     }
     printf("\n");
     i++;
}
}
You can also try this code with Online C Compiler
Run Code

Output

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Complexity Analysis

Time Complexity: O(N2)

Space Complexity: O(1)

Frequently Asked Questions

How can we make a half pyramid number pattern using C?

A nested for loop is used to print a half pyramid pattern in C language. The outer loop is used to iterate through each row and the inner loop iterates through each element of every row.

Which looping statements are used to print a half pyramid pattern?

The looping statements such as for loop, while and do-while loops in programming languages can be used to print a half pyramid pattern.

Conclusion

We learned how to code a program in C language to print a half pyramid number pattern in this blog. 

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass