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

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

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

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

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!