1.
Introduction
2.
Problem Statement
3.
Example
4.
Approach
5.
Program in C++
5.1.
Code
5.2.
Output
6.
Program in C
6.1.
Code
6.2.
Output
7.
Program in Java
7.1.
Code
7.2.
Output
8.
Program in Python
8.1.
Code
8.2.
Output
9.
FAQs
10.
Key Takeaways
Last Updated: Mar 27, 2024

# Full Pyramid Number Pattern: Part1

Muskan Gupta

## Introduction

A good programmer can write codes with full optimization. For this skill, knowledge of data structures and algorithms becomes essential. For this reason, the expertise in Data Structures and Algorithms (DSA) is often tested in discussions for SDE roles (Software Development Engineering). The importance of the DSA cannot be overemphasized, especially if you intend to work in product-based companies such as Google, Amazon, Microsoft, Adobe, etc.

For questions correlated to each data structure, refer to the blog Must do interview questions for product-based companies.

This blog will discuss the solutions to the interview problem: how to print the full pyramid number pattern in reverse order. We will code these solutions in C++CJava, and python

## Problem Statement

Write a program to print the full pyramid number pattern such that the  numbers are consecutive and in descending order ending at 1 in every row by taking the number of rows as inputs from the user.

## Example

Input

Number of rows: 5

Output

1

321

54321

7654321

987654321

Explanation

Here we can see that for the number of rows = 5, a pyramid is printed such that numbers are consecutive and in descending order ending at 1 in every row.

## 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 reverse order till 1.
• We can notice that the number at starting of ith row should be 2i-1 as per the given example in the problem statement section.

## Program in C++

Now we will solve this problem on how to print the full pyramid number pattern in reverse order 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=z;j>=1;j--){
cout<<j;
}
cout<<"\n";
}
return 0;
}
``````

### 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 in reverse order 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=z;j>=1;j--){
printf("%d",j);
}
printf("\n");
}
return 0;
}
``````

### 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 in reverse order 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=z;j>=1;j--){
System.out.print(j);
}
System.out.println();
}
sc.close();
}
}
``````

### 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 in reverse order 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(z,0,-1):
print(j,end="")
print()``````

### 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 2 and the Full Pyramid Number Pattern: Part 3

Also Read - Strong number in c

## FAQs

1. 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.

2. What will be the time complexity of the full pyramid number pattern part 1?
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).

3. What will be the space complexity of the full pyramid number pattern part 1?
O(1) will be the space complexity because no extra space was used.

## Key Takeaways

In this article, we have extensively discussed how to print the full pyramid number pattern in reverse order in C++, C,  Java, and python.

We hope that this blog has helped you enhance your knowledge on how to print the full pyramid number pattern in reverse order, and if you would like to learn more, check out our problemstest series, and top 150 interview puzzles. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass