Loop Assignment 23 — Print a Number Palindrome Pyramid Using Nested Loops
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Print a Number Palindrome Pyramid Using Nested Loops — Symmetric 1, 121, 12321 Pattern

📑 On this page:
  • How the Number Palindrome Pyramid Works
  • The Three Steps of Printing the Pyramid
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • How to print a number palindrome pyramid using nested loops
  • How to print increasing numbers from 1 to the current row
  • How to print decreasing numbers from the current row back to 1
  • How to print spaces to make the pyramid symmetrical
  • Why the middle number is printed only once
  • How nested loops create symmetric patterns

How the Number Palindrome Pyramid Works

A number palindrome pyramid is a symmetric pyramid in which each row reads the same from left to right and right to left. The first row is 1, the second row is 121, the third row is 12321, and so on.

Each row has two parts: an increasing part and a decreasing part. For example, row 4 contains 123 followed by 21. Together, they form 12321.

The program uses three loops for each row: one loop prints spaces, one loop prints increasing numbers, and another loop prints decreasing numbers. The outer loop controls the rows.

💡 In one line: Print spaces, then numbers from 1 to the current row, and finally numbers from the current row minus 1 back to 1.

Diagram showing a number palindrome pyramid 1, 121, 12321 printed using nested loops in C

The Three Steps of Printing the Pyramid

Step Loop operation Effect
1. Print spacesfor (space = 1; space <= rows - i; space++)Centers each row and creates the pyramid shape
2. Print increasing numbersfor (j = 1; j <= i; j++)Prints 1, 2, 3, and so on up to the current row number
3. Print decreasing numbersfor (j = i - 1; j >= 1; j--)Prints the numbers in reverse order back to 1

The increasing part of row 4 is 1234. The decreasing part is 321. When joined together, they form 1234321.

The middle number is printed only once. This is why the decreasing loop starts from i - 1, not from i. Otherwise, the middle number would be printed twice.

Complete C Program

Program: print a number palindrome pyramid using nested loops

#include <stdio.h>

int main(void) {
    int rows, i, j, space;

    printf("Enter the number of rows: ");
    if (scanf("%d", &rows) != 1 || rows <= 0) {
        printf("Invalid input. Please enter a positive integer.\n");
        return 1;
    }

    for (i = 1; i <= rows; i++) {
        /* Print leading spaces */
        for (space = 1; space <= rows - i; space++) {
            printf("  ");
        }

        /* Print increasing numbers */
        for (j = 1; j <= i; j++) {
            printf("%d ", j);
        }

        /* Print decreasing numbers */
        for (j = i - 1; j >= 1; j--) {
            printf("%d ", j);
        }

        printf("\n");
    }

    return 0;
}
                                    

Program Output

Enter the number of rows: 5

        1 
      1 2 1 
    1 2 3 2 1 
  1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
                                

A second run, with 4 rows:

Enter the number of rows: 4

      1 
    1 2 1 
  1 2 3 2 1 
1 2 3 4 3 2 1 
                                

Each row is symmetrical. For example, row 4 prints 1 2 3 4 3 2 1, which reads the same from both directions.

Step-by-Step Explanation

Let us understand how the program prints the palindrome pyramid for 5 rows.

1. The outer loop controls rows

The outer loop for (i = 1; i <= rows; i++) runs from row 1 to row 5. Each iteration prints one complete row of the pyramid.

2. Printing spaces

The first inner loop prints spaces before the numbers. The number of spaces decreases as the row number increases, which gives the output its pyramid shape.

3. Printing increasing numbers

The second inner loop prints numbers from 1 to i. For row 4, it prints 1 2 3 4.

4. Printing decreasing numbers

The third inner loop starts from i - 1 and prints numbers down to 1. For row 4, it prints 3 2 1. The value 4 is not repeated because it is already the middle number.

5. Example trace for row 4

  • Spaces: The program prints spaces to align the row.
  • Increasing part: 1 2 3 4.
  • Decreasing part: 3 2 1.
  • Complete row: 1 2 3 4 3 2 1.

6. Why the decreasing loop starts from i - 1

The increasing loop already prints the middle number, which is i. Therefore, the decreasing loop begins from i - 1 to avoid printing the middle number twice.

7. Why spaces are needed

Spaces shift each row toward the right. Without spaces, the numbers would start from the left edge and the output would not look like a pyramid.

8. Why the pattern is a palindrome

A palindrome reads the same forward and backward. Row 4, 1234321, reads the same from left to right and right to left. This is why the pattern is called a number palindrome pyramid.

9. Number of values in each row

Row i contains \(2i - 1\) numbers. For example, row 1 has 1 number, row 2 has 3 numbers, row 3 has 5 numbers, and row 4 has 7 numbers.

10. Complexity summary

Time complexity is O(n2), because the program prints a growing number of spaces and values for each row. Space complexity is O(1), because the program uses only loop variables and does not store the pyramid in an array.

🎯 Remember this: Print spaces first, then numbers from 1 to i, and finally numbers from i - 1 back to 1. This creates the symmetric palindrome pyramid.

Frequently Asked Questions

1. What is a number palindrome pyramid?

A number palindrome pyramid is a pyramid in which every row reads the same from left to right and right to left. Examples include 1, 121, 12321, and 1234321.

2. Why does the decreasing loop start from i - 1?

The increasing loop already prints the middle number i. Starting the decreasing loop from i - 1 prevents the middle number from being printed twice.

3. Why are spaces printed before the numbers?

Spaces shift each row to the right, which creates the pyramid shape. The number of spaces decreases as the row number increases.

4. How many numbers are printed in row i?

Row i contains \(2i - 1\) numbers. For example, row 3 contains 5 numbers: 1, 2, 3, 2, and 1.

5. Can this pattern be printed using only two loops?

Yes. You can use one loop for spaces and another loop that prints the increasing and decreasing parts together. However, using three separate loops makes the logic easier to understand for beginners.

6. What is the time complexity of this program?

The time complexity is O(n2), because the total number of spaces and numbers grows with the square of the number of rows. Space complexity is O(1), since no array is used.

💡 Tip: After implementing this program, try printing the same pyramid using alphabets, such as A, ABA, ABCBA. The loop structure remains the same; only the printed character changes.

📖 Related Tutorials

  • 22. Print a Plus-Sign Star Pattern Using Nested For Loops
  • 24. Print a Full Symmetric Number Pyramid Using Nested While Loops
  • 9. Print the Pattern of a Pyramid Using a For Loop
  • 13. Print the Pattern of a Diamond Using a For Loop
  • All Loop Assignments in C

Previous Topic: -->> 22. Write a program in C to print a plus-sign star pattern using nested for loops.   ||   Next Topic: -->> 24. Write a program in C to print a full symmetric number pyramid using nested while loops.


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.