Pyramid pattern using stars

Write a program to print all types of pyramids using stars.

Approach 1: Half Pyramid Pattern

C Program

#include <stdio.h>

int main()
{
    int rows;

    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Output:

Enter number of rows: 5 * * * * * * * * * * * * * * *
Approach 2: Inverted Half Pyramid Pattern

C Program

#include <stdio.h>

int main()
{
    int rows;

    printf("Enter number of rows: ");
    scanf("%d", &rows);

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

        printf("\n");
    }
    return 0;
}
Output:

Enter number of rows: 5 * * * * * * * * * * * * * * *
Approach 3: Full Pyramid Pattern

C Program

#include <stdio.h>

int main()
{
    int n;

    printf("Enter number of rows: ");
    scanf("%d", &n);

    for (int i = 0i < ni++)
    {
        for (int j = 0j < n - 1 - ij++)
        {
            printf(" ");
        }
        for (int j = n - 1 - ij <= n - 1 + ij++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}
Output:

Enter number of rows: 5 * *** ***** ******* *********

Approach 4: Full Inverted Pyramid Pattern

C Program

#include <stdio.h>

int main()
{
    int n;

    printf("Enter number of rows: ");
    scanf("%d", &n);

    int space = 0;
    for (int i = 0i < ni++)
    {

        for (int j = 0j < spacej++)
        {
            printf(" ");
        }
        for (int j = 1j <= 2 * (n - i) - 1j++)
        {
            printf("*");
        }
        printf("\n");
        space++;
    }
    return 0;
}
Output:

Enter number of rows: 5 ********* ******* ***** *** *


No comments:

Post a Comment