Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
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: Part2

Author Muskan Gupta
0 upvote

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 inverted full pyramid number patternWe will code these solutions in C++CJava, and Python

Problem Statement

Write a program to print the inverted full pyramid number pattern such that if the number of rows is n, then the ith row will have n-i+1 printed (n-i+1) times with spaces in front and between the numbers, where 1=<i<=n. n is the input we get from the user.

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

  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 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).
     
  3. 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 problemstest series, and top 150 interview puzzles. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass