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.