Loop Assignment 24 — Print a Full Symmetric Number Pyramid Using Nested While Loops
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Print a Full Symmetric Number Pyramid Using Nested While Loops — Symmetric Number Pattern

šŸ“‘ On this page:
  • How the Symmetric Number 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 full symmetric number pyramid using nested while loops
  • How to print spaces to center each row
  • 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 control loop variables carefully in while loops
  • How this pattern differs from a palindrome number pyramid

How the Symmetric Number Pyramid Works

A full symmetric number pyramid prints the same number repeatedly in each row. For example, row 1 contains one 1, row 2 contains two 2s, row 3 contains three 3s, and so on.

The pyramid is symmetric because spaces are printed before the numbers. The number of spaces decreases as the row number increases, which shifts each row toward the center.

This program uses nested while loops. The outer while loop controls the rows, the first inner while loop prints spaces, and the second inner while loop prints the numbers.

šŸ’” In one line: Print spaces to center the row, then print the current row number repeatedly, and move to the next line.

Diagram showing a full symmetric number pyramid printed using nested while loops in C

The Three Steps of Printing the Pyramid

Step While loop operation Effect
1. Print spaceswhile (space <= rows - i)Centers each row by printing decreasing spaces
2. Print numberswhile (j <= i)Prints the current row number i exactly i times
3. Move to the next rowprintf("\n");Starts a new line after completing the current row

In this pattern, row 1 prints one 1, row 2 prints two 2s, row 3 prints three 3s, and row 4 prints four 4s. The value printed in each row is equal to the row number.

Unlike a palindrome pyramid, this pattern does not print increasing and decreasing numbers. It prints the same number repeatedly within each row.

Complete C Program

Program: print a full symmetric number pyramid using nested while 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;
    }

    i = 1;

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

        /* Print the current row number */
        j = 1;
        while (j <= i) {
            printf("%d   ", i);
            j++;
        }

        printf("\n");
        i++;
    }

    return 0;
}
                                    

Program Output

Enter the number of rows: 5

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

A second run, with 4 rows:

Enter the number of rows: 4

      1   
    2   2   
  3   3   3   
4   4   4   4   
                                

The number of spaces decreases in every row, while the number of printed values increases. This creates the full symmetric pyramid shape.

Step-by-Step Explanation

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

1. The outer while loop controls rows

The variable i starts at 1 and continues while i <= rows. Each iteration prints one complete row of the pyramid.

2. Printing spaces

The first inner while loop prints spaces. For row 1, it prints 4 groups of spaces. For row 2, it prints 3 groups, and this continues until row 5 prints no spaces.

3. Printing numbers

The second inner while loop prints the current row number. For row 3, it prints 3 three times. For row 5, it prints 5 five times.

4. Moving to the next line

After the number-printing loop finishes, printf("\n"); moves the output to the next line. Then i++ prepares the program for the next row.

5. Example trace for row 3

  • The program prints spaces to align the row.
  • The number-printing loop runs three times because i = 3.
  • It prints 3 3 3.
  • The program prints a newline and moves to row 4.

6. Why spaces decrease

The condition space <= rows - i prints fewer spaces as i increases. This shift makes the pyramid appear centered and symmetrical.

7. Why the number equals the row number

In this pattern, the value printed in each row is the row number itself. Therefore, row 4 prints 4, row 5 prints 5, and so on.

8. Importance of resetting loop variables

The variables space and j must be reset at the beginning of every row. If they are not reset, the loops will not print the correct number of spaces or values.

9. While loop vs for loop

A for loop combines initialization, condition, and update in one line. A while loop separates them, so the programmer must initialize the variable before the loop and update it inside the loop. This program demonstrates that manual control.

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 a few integer variables and does not store the pyramid in an array.

šŸŽÆ Remember this: Use one while loop for rows, one for spaces, and one for numbers. Print the row number repeatedly, decrease the spaces row by row, and print a newline after each row.

Frequently Asked Questions

1. What is a full symmetric number pyramid?

A full symmetric number pyramid is a pyramid in which each row contains the same number repeated multiple times. Row 1 has one 1, row 2 has two 2s, row 3 has three 3s, and so on.

2. Why are nested while loops used?

Nested while loops are used because the program must handle rows, spaces, and numbers separately. The outer loop controls rows, while the inner loops print spaces and values.

3. Why must space and j be reset?

Both variables must be reset at the beginning of every row. Otherwise, their values from the previous row will affect the current row and produce an incorrect pattern.

4. How is this different from a palindrome pyramid?

A palindrome pyramid prints increasing and then decreasing numbers, such as 12321. A symmetric number pyramid prints the same number repeatedly, such as 333 in row 3.

5. Can this program be written using for loops?

Yes. The same output can be produced using nested for loops. The while-loop version is useful for understanding loop initialization, condition checking, and manual incrementing.

6. What is the time complexity of this program?

The time complexity is O(n2), because the total number of spaces and printed values grows with the square of the number of rows. Space complexity is O(1).

šŸ’” Tip: After implementing this program, try printing the same pyramid using stars, alphabets, or even numbers. The loop structure remains the same; only the printed value changes.

šŸ“– Related Tutorials

  • 23. Print a Number Palindrome Pyramid Using Nested Loops
  • 25. Calculate the Sum of the Series 1 + 1/2² + 1/3² + … + 1/N² Using a For Loop
  • 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: -->> 23. Write a program in C to print a number palindrome pyramid (e.g., 1, 121, 12321) using nested loops.   ||   Next Topic: -->> 25. Write a program in C to calculate the sum of the series 1 + 1/2² + 1/3² + … + 1/N² using a for loop.


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.