Table of contents
1.
Introduction
2.
Half Pyramid in C
3.
Half Pyramid in C++
4.
Half Pyramid in Java
5.
Half Pyramid in Python
6.
Frequently Asked Questions
6.1.
What is a loop in programming?
6.2.
What are the different types of loops?
6.3.
Is there no for loop in Python?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Half Pyramid

Author Aditi
0 upvote
Interview Puzzles

Introduction

Half pyramid is a pattern, and it is in a triangle shape. In this blog, we will implement half pyramid of numbers using programming languages C, C++, Java, and Python. The pattern will be made with non-repetitive numbers. But before moving on to the blog, first clearance of basic loops is essential. You can refer to loops in C/C++loops in Java, and loops in Python.

Half Pyramid in C

Problem Statement: Write a program to print a half pyramid where numbers are non-repetitive in C.

Solution:

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

Output:

Size of row :5
1
12
123
1234
12345
You can also try this code with Online C Compiler
Run Code

Half Pyramid in C++

Problem Statement: Write a program to print a half pyramid where numbers are non-repetitive in C++.

Solution:

#include <iostream>
using namespace std;
int main()
{
  cout<<"Enter the size of row : 5\n";
 for(int i=1;i<=row;i++)
  {
      for(int j=1;j<=i;j++)
      {
           cout<<j;
      }
      cout<<"\n";
  }
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Size of row :5
1
12
123
1234
12345
You can also try this code with Online C++ Compiler
Run Code

Half Pyramid in Java

Problem Statement: Write a program to print half pyramid where numbers are non-repetitive in Java.

Solution:

public class pattern {

 public static void main(String[] args) {
    System.out.println("Size of row : 5");

    for(int i=1;i<=5;i++)
    {
      for(int j=1;j<=i;j++)
      {
          System.out.print(j);
      }
      System.out.println();
    }
 }
}
You can also try this code with Online Java Compiler
Run Code

Output :

Size of row :5
1
12
123
1234
12345
You can also try this code with Online Java Compiler
Run Code

Must Read What are Loops in Java.

Half Pyramid in Python

Problem Statement: Write a program to print a half pyramid where numbers are non-repetitive in Python.

Solution:

print(“Size of row :5”)
for i in range(1,6):
    for j in range(1,i+1,+1):
        print(j,end="")
    print("\r")
You can also try this code with Online Python Compiler
Run Code

Output :

Size of row :5
1
12
123
1234
12345
You can also try this code with Online Python Compiler
Run Code

Frequently Asked Questions

What is a loop in programming?

Loops are sequences of program lines that need to be repeated until some condition is fulfilled.

What are the different types of loops?

There are two types of loops: entry control and exit control loops. 

Is there no for loop in Python?

There is for in loop in Python instead of for loop.

Conclusion

In this article, we have extensively discussed the implementation of a half pyramid with non-repetitive numbers in different programming languages. 

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, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Interview Puzzles, 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