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: Part3

Author Muskan Gupta
1 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 this interview problem: how to print the full pyramid number patternWe 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 increasing order starting from 1 in every row by taking the number of rows as inputs from the user.

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

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

Live masterclass