Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution Approach
3.1.
Implementation in C
3.2.
Implementation in C++
3.3.
Implementation in Java
3.4.
Implementation in Python
4.
Frequently Asked Questions
4.1.
What is a puzzle?
4.2.
How to solve a number sequence puzzle?
4.3.
What are the advantages of solving puzzles?
4.4.
What is the time complexity of generating the full pyramid of odd number patterns?
4.5.
Which are the commonly asked pattern-solving problems in interviews?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

The Full Pyramid Of Odd Number Pattern

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

Introduction

The Odd Number Pattern Puzzle is one of the easiest interview puzzle questions. It involves printing the odd numbers in a sequence in form of a pattern.

Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.

These types of questions are often asked in interviews. So, we will be going through the problem statement and then analyzing it to get a better understanding.

Problem Statement

Write a program to print the full pyramid of odd number pattern puzzles. The pattern is shown below:

        1

      333

    55555

  7777777

999999999

Solution Approach

We will use the loops to solve the full pyramid of odd number patterns. The following steps have to be performed to obtain the pattern.

  • Take an outer ‘for’ loop to print all the rows.
  • Take two inner ‘for’ loops to print the first half of the spaces, and the second half to print the odd number.
  • We can increment the odd number by 2 after every iteration of the row. We will initialize the number as 1.

Implementation in C

Here is the source code of the C program to print the full pyramid of odd number patterns.

#include <stdio.h>
int main() {
   printf("Enter the row size:");
   int row_size;
   int num = 1;
   scanf("%d",&row_size);
   for(int j = 0; j < row_size; j++) {
       for(int i = row_size - 1; i > j; i--) {
           printf(" ");
       }
       for(int i = 0; i < num; i++) {
           printf("%d",num);
       }
       num += 2;
       printf("\n");
   }
}
You can also try this code with Online C Compiler
Run Code

Output

Output

Implementation in C++

Here is the source code of the C++ program to print the full pyramid of odd number pattern.

#include <iostream>
using namespace std;

int main() {
   printf("Enter the row size:");
   int row_size;
   int num = 1;
   cin>>row_size;
   for(int j = 0; j < row_size; j++) {
      for(int i = row_size - 1; i > j; i--) {
          cout<<" ";
      }
      for(int i = 0; i < num; i++) {
          cout<<num;
      }
      num += 2;
      cout<<"\n";
   }
}
You can also try this code with Online C++ Compiler
Run Code

Output

Output

To learn C++, check out our C++ guided path.

Implementation in Java

Here is the source code of the Java program to print the full pyramid of odd number pattern.

import java.util.Scanner;

public class CodingNinjas {
   public static void main(String args[]) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the row size:");
       int num = 1;
       int rowSize = sc.nextInt();
       for(int j = 0; j < rowSize; j++) {
          for(int i = rowSize - 1; i > j; i--) {
              System.out.print(" ");
          }
          for(int i = 0; i < num; i++) {
              System.out.print(num);
          }
          num += 2;
          System.out.println();
       }
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

Output

To learn Java, check out our Java guided path.

Implementation in Python

Here is the source code of the Python program to print the full pyramid of odd number pattern.

print("Enter the row size: ")
num = 1
row_size = int(input())
for j in range(row_size):
   i=row_size-1
   while i>j:
       print(" ", end="")
       i=i-1
   for i in range(num):
       print(num, end="")
   num=num+2
   print()
   
You can also try this code with Online Python Compiler
Run Code

Output

Output

Frequently Asked Questions

What is a puzzle?

A puzzle is a game of words, toys, questions, etc. that helps in increasing our problem-solving ability.

How to solve a number sequence puzzle?

To solve a number sequence puzzle, first, find the pattern between numbers of the sequence. Once you know the pattern then you can find the next number of the sequence by following the same pattern.

What are the advantages of solving puzzles?

Solving puzzles enhances the problem-solving skills of a person. It also improves the logic building of a person.

What is the time complexity of generating the full pyramid of odd number patterns?

The time complexity of generating the full pyramid of odd number patterns is O(row_size2).

Which are the commonly asked pattern-solving problems in interviews?

In interviews, the commonly asked pattern-solving problems are Hollow Diamond Star Pattern, Inverted V-shape Number Pattern, Matrix in Reverse Spiral Form, etc.

Conclusion

In this article, we discussed the interview-based puzzle. The puzzle is based on implementing the full pyramid of odd number pattern as shown above in the article. We solved this problem and the article shows the implementation in different programming languages.

Apart from this, you can also expand your knowledge by referring to Interview Preparation Course. 

For more information, refer to Free Interview Preparation resources.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass