C Programs to Print Pyramid Patterns Using Nested Loops

In C programming, pattern programs are commonly used to understand nested loops. These programs print different patterns of stars, numbers, and alphabets using for loops.

A nested loop is a loop inside another loop. The outer loop controls the number of rows, while the inner loop controls the number of characters, numbers, or spaces printed in each row.

C Programming में Pattern Programs का उपयोग Nested Loops को समझने के लिए किया जाता है। इन programs की सहायता से हम Stars, Numbers और Alphabets के विभिन्न patterns print कर सकते हैं।

Nested Loop एक ऐसा loop होता है जिसके अंदर दूसरा loop होता है। Outer Loop rows को नियंत्रित करता है, जबकि Inner Loop प्रत्येक row में print होने वाले characters, numbers या spaces की संख्या को नियंत्रित करता है।

Basic Structure of Nested Loops

for (i = 1; i <= rows; i++)
{
    for (j = 1; j <= columns; j++)
    {
        // Print pattern
    }

    printf("\n");
}

Nested Loops का Basic Structure

for (i = 1; i <= rows; i++)
{
    for (j = 1; j <= columns; j++)
    {
        // Pattern print करें
    }

    printf("\n");
}

In the following programs, we use five rows to print different patterns. You can change the number of rows as required.

निम्नलिखित programs में विभिन्न patterns print करने के लिए पाँच rows का उपयोग किया गया है। आवश्यकता के अनुसार rows की संख्या बदली जा सकती है।

1. Program to Print a Left-Aligned Increasing Star Pattern

Pattern:

*
**
***
****
*****

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Stars\n");

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Explanation

The outer loop runs from 1 to 5. The inner loop prints stars according to the current row number.

For example, the first row prints one star, the second row prints two stars, and the fifth row prints five stars.

Outer Loop 1 से 5 तक चलता है। Inner Loop प्रत्येक row के number के अनुसार stars print करता है।

पहली row में एक star, दूसरी row में दो stars और पाँचवीं row में पाँच stars print होते हैं।

2. Program to Print an Increasing Number Pattern

Pattern:

1
12
123
1234
12345

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Numbers\n");

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Explanation

The inner loop prints numbers starting from 1 up to the current row number.

Inner Loop प्रत्येक row में 1 से लेकर current row number तक numbers print करता है।

3. Program to Print a Repeated Number Pattern

Pattern:

1
22
333
4444
55555

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Numbers\n");

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;
}

Explanation

The outer loop value i is printed repeatedly by the inner loop. The number of repetitions increases in each row.

Outer Loop का variable i Inner Loop द्वारा बार-बार print किया जाता है। प्रत्येक row में number की repetitions बढ़ती जाती हैं।

4. Program to Print a Decreasing Star Pattern

Pattern:

*****
****
***
**
*

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Stars\n");

    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Explanation

The outer loop decreases from 5 to 1. Therefore, the number of stars decreases in every row.

Outer Loop 5 से 1 तक decrease होता है। इसलिए प्रत्येक row में stars की संख्या कम होती जाती है।

5. Program to Print a Decreasing Number Pattern

Pattern:

12345
1234
123
12
1

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Numbers\n");

    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Explanation

The outer loop controls the number of values printed in each row. The inner loop prints numbers from 1 up to i.

Outer Loop प्रत्येक row में print होने वाले numbers की संख्या को नियंत्रित करता है। Inner Loop 1 से i तक numbers print करता है।

6. Program to Print a Decreasing Repeated Number Pattern

Pattern:

55555
4444
333
22
1

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Numbers\n");

    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;
}

Explanation

The outer loop decreases from 5 to 1. The inner loop prints the current value of i as many times as its value.

Outer Loop 5 से 1 तक decrease होता है। Inner Loop current value of i को उतनी ही बार print करता है।

7. Program to Print a Continuous Number Pattern

Pattern:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

C Program

#include <stdio.h>

int main()
{
    int i, j, k = 1;

    printf("Pyramid of Numbers\n");

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d ", k);
            k++;
        }

        printf("\n");
    }

    return 0;
}

Explanation

The variable k is initialized to 1. After printing each number, its value is increased by 1.

Variable k को 1 से initialize किया गया है। प्रत्येक number print होने के बाद इसकी value 1 से बढ़ा दी जाती है।

8. Program to Print a Reverse Continuous Number Pattern

Pattern:

15
14 13
12 11 10
9 8 7 6
5 4 3 2 1

C Program

#include <stdio.h>

int main()
{
    int i, j, k = 15;

    printf("Pyramid of Numbers\n");

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d ", k);
            k--;
        }

        printf("\n");
    }

    return 0;
}

Explanation

The variable k starts from 15 and decreases by 1 after each printed number. The number of values printed in each row increases from 1 to 5.

Variable k की शुरुआत 15 से होती है और प्रत्येक number print होने के बाद इसकी value 1 से कम होती जाती है। प्रत्येक row में numbers की संख्या 1 से 5 तक बढ़ती जाती है।

9. Program to Print a Centered Increasing Star Pyramid

Pattern:

    *
   ***
  *****
 *******
*********

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Stars\n");

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= 2 * i - 1; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Explanation

This pattern uses two inner loops:

  • The first inner loop prints spaces before the stars.

  • The second inner loop prints stars in odd numbers: 1, 3, 5, 7, and 9.

इस pattern में दो Inner Loops का उपयोग किया गया है:

  • पहला Inner Loop stars से पहले spaces print करता है।

  • दूसरा Inner Loop 1, 3, 5, 7 और 9 stars print करता है।

10. Program to Print a Centered Decreasing Star Pyramid

Pattern:

*********
 *******
  *****
   ***
    *

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Stars\n");

    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= 2 * i - 1; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Explanation

The outer loop decreases from 5 to 1. The first inner loop prints spaces, and the second inner loop prints stars in decreasing odd numbers.

Outer Loop 5 से 1 तक decrease होता है। पहला Inner Loop spaces और दूसरा Inner Loop decreasing odd numbers में stars print करता है।

11. Program to Print a Decreasing Repeated Number Pattern

Pattern:

11111
2222
333
44
5

C Program

#include <stdio.h>

int main()
{
    int i, j, k = 1;

    printf("Pyramid of Numbers\n");

    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d", k);
        }

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

    return 0;
}

Explanation

The outer loop controls the number of digits printed in each row. The variable k changes after each row.

Outer Loop प्रत्येक row में digits की संख्या को नियंत्रित करता है। प्रत्येक row के बाद variable k की value बढ़ती है।

12. Program to Print a Right-Aligned Increasing Star Pattern

Pattern:

    *
   **
  ***
 ****
*****

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Stars\n");

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Explanation

The first inner loop prints spaces, and the second inner loop prints stars. The number of spaces decreases as the number of stars increases.

पहला Inner Loop spaces और दूसरा Inner Loop stars print करता है। Stars की संख्या बढ़ने के साथ spaces की संख्या कम होती जाती है।

13. Program to Print a Right-Aligned Decreasing Star Pattern

Pattern:

*****
 ****
  ***
   **
    *

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Pyramid of Stars\n");

    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Explanation

The outer loop decreases the number of stars in each row. The first inner loop increases the spaces before the stars.

Outer Loop प्रत्येक row में stars की संख्या कम करता है। पहला Inner Loop stars से पहले spaces की संख्या बढ़ाता है।

14. Program to Print an Alphabet Pattern

Pattern:

A
BC
DEF
GHIJ
KLMNO

C Program

#include <stdio.h>

int main()
{
    int i, j;
    char k = 'A';

    printf("Pyramid of Alphabets\n");

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%c", k);
            k++;
        }

        printf("\n");
    }

    return 0;
}

Explanation

The variable k is a character initialized to 'A'. After each character is printed, its value is incremented to get the next alphabet character.

Variable k एक character है जिसे 'A' से initialize किया गया है। प्रत्येक character print होने के बाद इसकी value increment होती है और अगला alphabet print होता है।

15. Program to Print a Diamond Star Pattern

Pattern:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Diamond Pattern of Stars\n");

    // Upper half
    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= 2 * i - 1; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    // Lower half
    for (i = 4; i >= 1; i--)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= 2 * i - 1; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Explanation

The diamond pattern is divided into two parts:

  • Upper half: prints an increasing star pyramid.

  • Lower half: prints a decreasing star pyramid.

Diamond Pattern को दो parts में divide किया गया है:

  • Upper Half: Increasing Star Pyramid print करता है।

  • Lower Half: Decreasing Star Pyramid print करता है।

16. Program to Print an Hourglass Star Pattern

Pattern:

*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********

C Program

#include <stdio.h>

int main()
{
    int i, j;

    printf("Hourglass Pattern of Stars\n");

    // Upper half
    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= 2 * i - 1; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    // Lower half
    for (i = 2; i <= 5; i++)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= 2 * i - 1; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Explanation

The hourglass pattern also contains two parts:

  • Upper half: prints a decreasing star pyramid.

  • Lower half: prints an increasing star pyramid.

Hourglass Pattern में भी दो parts होते हैं:

  • Upper Half: Decreasing Star Pyramid print करता है।

  • Lower Half: Increasing Star Pyramid print करता है।

Important Points to Remember

  1. The outer loop controls the number of rows.

  2. The inner loop controls the number of stars, numbers, alphabets, or spaces.

  3. Use printf("\n"); to move to the next row.

  4. Use an increasing loop to print increasing patterns.

  5. Use a decreasing loop to print decreasing patterns.

  6. Patterns that require alignment usually use spaces before printing stars.

  7. Diamond and hourglass patterns can be created by combining two different loops.

याद रखने योग्य महत्वपूर्ण बातें

  1. Outer Loop rows की संख्या को नियंत्रित करता है।

  2. Inner Loop stars, numbers, alphabets या spaces की संख्या को नियंत्रित करता है।

  3. अगली row में जाने के लिए printf("\n"); का उपयोग किया जाता है।

  4. Increasing patterns के लिए increasing loop का उपयोग करें।

  5. Decreasing patterns के लिए decreasing loop का उपयोग करें।

  6. Alignment वाले patterns में stars से पहले spaces print किए जाते हैं।

  7. Diamond और Hourglass patterns को दो अलग-अलग loops को combine करके बनाया जा सकता है।

Pyramid pattern programs are useful for learning nested loops in C. By changing the starting value, ending value, loop condition, and printed character, we can create many different patterns using the same basic logic.

C में Pyramid Pattern Programs Nested Loops सीखने के लिए उपयोगी हैं। Starting Value, Ending Value, Loop Condition और print होने वाले character को बदलकर इसी basic logic की सहायता से अनेक patterns बनाए जा सकते हैं।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025