Example
Input
Number of rows: 5
Output
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Explanation
Here, a pyramid is printed such that if the number of rows is n=5, then the ith row has 6-i printed (6-i) times with spaces in front and between the numbers, where 1=<i<=n.
Approach
Firstly we will take the input for the number of rows. And to print the inverted 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 i-1 at the starting of ith row.
- And the second inner loop will be printing the number n-i+1, (n-i+1) times.
Program in C++
Now we will solve this problem on how to print the inverted 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;
int space=0;
for(int i=n;i>=1;i--){
for(int j=0;j<space;j++){
cout<<" ";
}
space++;
for(int j=0;j<i;j++){
cout<<i<<" ";
}
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 inverted 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,space=0;
for(i=n;i>=1;i--){
for(j=0;j<space;j++){
printf(" ");
}
space++;
for(j=0;j<i;j++){
printf("%d ",i);
}
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 inverted 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();
int space=0;
for(int i=n;i>=1;i--){
for(int j=0;j<space;j++){
System.out.print(" ");
}
space++;
for(int j=0;j<i;j++){
System.out.print(i);
System.out.print(" ");
}
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 inverted full pyramid number pattern by taking the number of rows as inputs from the user in Python.
Code
n=int(input("Number of rows: "))
space=0
for i in range(n,0,-1):
for j in range(space):
print(" ",end="")
space+=1
for j in range(i):
print(i,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 3 .
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 inverted full pyramid number pattern program?
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 inverted full pyramid number pattern program?
O(1) will be the space complexity because no extra space was used.
Key Takeaways
This article extensively discussed how to print the inverted full pyramid number pattern in C++, C, Java, and Python.
We hope that this blog has helped you enhance your knowledge of printing the inverted 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!