Example
Input
Number of rows: 5
Output
1
123
12345
1234567
123456789
Explanation
Here we can see that for the number of rows = 5, a pyramid is printed such that numbers are consecutive and in increasing order starting from 1 in every row. The last digit of the ith row is 2i-1, where 1=<i<= the number of rows.
Approach
Firstly we will take the input for the number of rows. And to print the full pyramid number pattern, we will use two for loops. And, we will calculate the number of times both the loops should run and what they should it print.
- If the number of rows is n, then the outer loop will run for n times, and there will be two inner loops.
- One inner loop will ensure the spaces before the starting number of every row.
- The number of spaces will be n-i for the ith row.
- And the second inner loop will be printing the numbers in increasing order till 2i-1.
Program in C++
Now we will solve this problem on how to print the full pyramid number pattern by taking the number of rows as inputs from the user in C++.
Code
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cout<<"Number of rows: ";
cin>>n;
for(int i=1;i<=n;i++){
int y=n-i,z=2*i;
for(int j=0;j<y;j++){
cout<<" ";
}
z--;
for(int j=1;j<=z;j++){
cout<<j;
}
cout<<"\n";
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output
If you want to learn C++ from basic level to completion check out our, Guided path for learning C++ .
Program in C
Now we will solve this problem on how to print the full pyramid number pattern by taking the number of rows as inputs from the user in C.
Code
#include<stdio.h>
int main() {
int n=0;
printf("Number of rows: ");
scanf("%d",&n);
int i=0,j=0;
for(i=1;i<=n;i++){
int y=n-i,z=2*i;
for(j=0;j<y;j++){
printf(" ");
}
z--;
for(j=1;j<=z;j++){
printf("%d",j);
}
printf("\n");
}
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output
If you want to learn C from check out our, C language blogs
Program in Java
Now we will solve this problem on how to print the full pyramid number pattern by taking the number of rows as inputs from the user in Java.
Code
import java.util.*;
class CodingNinjas
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.print("Number of rows: ");
int n=sc.nextInt();
for(int i=1;i<=n;i++){
int y=n-i,z=2*i;
for(int j=0;j<y;j++){
System.out.print(" ");
}
z--;
for(int j=1;j<=z;j++){
System.out.print(j);
}
System.out.println();
}
sc.close();
}
}
You can also try this code with Online Java Compiler
Run Code
Output
If you want to learn Java from basic level to completion check out our, Guided path for learning Java.
Program in Python
Now we will solve this problem on how to print the full pyramid number pattern by taking the number of rows as inputs from the user in python.
Code
n=int(input("Number of rows: "))
for i in range(1,n+1):
y=n-i
z=(2*i)-1
for j in range(y):
print(" ",end="")
for j in range(1,z+1):
print(j,end="")
print()
You can also try this code with Online Python Compiler
Run Code
Output
If you want to learn Python from basic level to completion check out our, Guided path for learning Python
You can also check out the Full Pyramid Number Pattern: Part 1 and the Full Pyramid Number Pattern: Part 2.
FAQs
-
What is the benefit of pattern puzzles?
Solving a pattern puzzle is the best way to learn the use of loop statements and practice some logical problems.
-
What will be the time complexity of the full pyramid number pattern part3?
O(n2) will be the time complexity because the inner loops will run ((n)+(n-1)+...+1) times which is n*(n+1)/2. So, the time complexity is O(n2).
-
What will be the space complexity of the full pyramid number pattern part3?
O(1) will be the space complexity because no extra space was used.
Key Takeaways
This article extensively discussed printing the full pyramid number pattern in C++, C, Java, and Python.
We hope that this blog has helped you enhance your knowledge of printing the full pyramid number pattern, and if you would like to learn more, check out our problems, test series, and top 150 interview puzzles. Do upvote our blog to help other ninjas grow. Happy Coding!