Table of contents
1.
Introduction
2.
Problem Statement
2.1.
Program in C 
2.2.
Related Problems
2.2.1.
Print Spaced Full Pyramid Star Pattern
2.2.2.
Print pyramid of stars in reverse direction
2.3.
Program in C++
2.4.
Another Method
2.5.
Program in Java
2.6.
Program in Python
2.6.1.
Related Examples
3.
Frequently Asked Questions
3.1.
What is pyramid in C programming?
3.2.
How to print an alphabet pyramid in C?
3.3.
What is pattern programming in C?
4.
Conclusion
Last Updated: Aug 20, 2024
Medium

C Program to Print Pyramids and Patterns

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The different programming languages are spaced full Pyramid pattern of numbers will be discussed in this article. Pyramid designs are all made out of polygons. The interviewer frequently asks these questions to test the programmer's logical and reasoning abilities. We can develop multiple pyramid patterns in C, C++JAVAPYTHONPHP, and other computer languages once we grasp the logic of the code.

Illustration

Now we'll use loops and if expressions to generate numerous pyramid patterns in different programming languages.

Problem Statement

Write a program that prints the spaced full pyramid number pattern.

For this program, we have sample inputs as given below:

Sample Input 1

Sample Output 1

7

  1

   2 2

  3 3 3

  4 4 4 4

  5 5 5 5 5

 6 6 6 6 6 6

7 7 7 7 7 7 7 

Sample Input 2

Sample Output 2

3

  1

   2 2

  3 3 3

 

For this program, this data is required:

For input myInput
For output myOutput
Addition Information (While using Python)  We need x and y.

 

Program in C 

The source code for the C program that prints the spaced full pyramid number pattern is mentioned below:

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

 

Output 

Output

Related Problems

Print Spaced Full Pyramid Star Pattern

#include <stdio.h>
int main() {
  int i, space, rows, k = 0;
  printf("Enter the size of the rows: ");
  scanf("%d", &rows);
  for (i = 1; i <= rows; ++i, k = 0) {
      for (space = 1; space <= rows - i; ++space) {
        printf("  ");
      }
      while (k != 2 * i - 1) {
        printf("* ");
        ++k;
      }
      printf("\n");
  }
  return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Output

Print pyramid of stars in reverse direction

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

 

Output

Output

Program in C++

The source code for the C++ program that prints the spaced full pyramid number pattern is mentioned below:

#include<iostream>
using namespace std;
int main()
{
    int myOutput,x,y;
    cout<<"Enter the size of the row: "<<endl;
      int myInput;
      cin>>myInput;
      for(myOutput=1;myOutput<=myInput;myOutput++)
      {
      for(x=myInput-1;x>=myOutput;x--)
      {
        cout<<" ";
      }
        for(y=1;y<=myOutput;y++)
        {
          cout<<myOutput<<" ";
        }
        cout<<"\n";
}
}
You can also try this code with Online C++ Compiler
Run Code

 

Output 

Output

Another Method

#include <iostream>
using namespace std;
int main()
{
    int rows, count = 0, count1 = 0, k = 0;
    cout << "Enter the size of rows: ";
    cin >> rows;
    for(int i = 1; i <= rows; ++i)
    {   for(int space = 1; space <= rows-i; ++space)
        {
            cout << "  ";
            ++count;
        }
        while(k != 2*i-1)
        {   if (count <= rows-1)
            {
                cout << i+k << " ";
                ++count;
            }
            else
            {
                ++count1;
                cout << i+k-2*count1 << " ";
            }
            ++k;
        }
        count1 = count = k = 0;
        cout << endl;
    }
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Output

Program in Java

The source code for the Java program that prints the spaced full pyramid number pattern is mentioned below:

import java.util.Scanner;
public class myPyramidNumberPattern {

  public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
        int myOutput,x,y;
      System.out.println("Enter the size of the row: ");
      int myInput=s.nextInt();
      for(myOutput=1;myOutput<=myInput;myOutput++)
      {
      for(x=myInput-1;x>=myOutput;x--)
      {
        System.out.printf(" ");
      }
        for(y=1;y<=myOutput;y++)
        {
          System.out.print(myOutput+" ");
        }
        System.out.println();
      }
      s.close();
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output 

Output

Program in Python

The source code for the Python program that prints the spaced full pyramid number pattern is mentioned below:

myInput=int(input("Enter the size of the row:\n"))
for myOutput in range(1,myInput+1):
    for x in range(myInput,myOutput,-1):
        print(" ",end="")
    for y in range(1,myOutput+1):
        print(myOutput,end=" ")
    print("\r")
You can also try this code with Online Python Compiler
Run Code

 

Output 

Output

Related Examples

Print the pattern which is given below:

            1 

         2 3 2 

      3 4 5 4 3 

   4 5 6 7 6 5 4 

5 6 7 8 9 8 7 6 5

rows = int(input("Enter the size of the rows: "))
k = 0
count=0
count1=0
for i in range(1, rows+1):
    for space in range(1, (rows-i)+1):
        print("  ", end="")
        count+=1
    while k!=((2*i)-1):
        if count<=rows-1:
            print(i+k, end=" ")
            count+=1
        else:
            count1+=1
            print(i+k-(2*count1), end=" ")
        k += 1
    count1 = count = k = 0
    print()
You can also try this code with Online Python Compiler
Run Code

 

Output

Output

Frequently Asked Questions

What is pyramid in C programming?

A pyramid in C programming is a pattern of characters or numbers arranged in a triangular shape, typically used to demonstrate nested loops and formatting. It often involves aligning output to form a visually appealing, symmetrical structure.

How to print an alphabet pyramid in C?

To print an alphabet pyramid in C, use nested loops. The outer loop controls the number of rows, while inner loops manage spaces and characters. For example, printing the letters A to Z in a pyramid shape requires careful control of spacing and character output.

What is pattern programming in C?

Pattern programming in C involves creating various patterns or designs using nested loops. It’s used to practice loop control structures, understand algorithms, and display output in structured formats like stars, numbers, or characters, enhancing logical and programming skills.

Conclusion

In this article, we have discussed the spaced full pyramid number pattern. We have seen the solution in different languages like C, C++, Java, and Python.

Recommended Readings:


Do check out some of the Popular Interview Problems on Code360. Do upvote our blog to help other ninjas grow. 

Live masterclass