Pyramid pattern using numbers

Write a program for the Pyramid pattern using numbers.

Approach 1: Half Pyramid Pattern

C Program

#include <stdio.h>

int main()
{
    int rows;
    int num;

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

    printf("Enter a number which is printed in pattern: ");
    scanf("%d", &num);

    for (int i = 1i <= rowsi++)
    {
        for (int j = 1j <= ij++)
        {
            printf("%2d"num);
        }
        printf("\n");
    }
    return 0;
}
Output:

Enter number of rows: 5 Enter a number which is printed in pattern: 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
Approach 2: Inverted Half Pyramid Pattern
C Program
#include <stdio.h>

int main()
{
    int rows;
    int num;

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

    printf("Enter a number which is to be printed in pattern: ");
    scanf("%d", &num);

    for (int i = 1i <= rowsi++)
    {
        for (int j = 1j <= rows - i + 1j++)
        {
            printf("%2d"num);
        }

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

Enter number of rows: 5 Enter a number which is to be printed in pattern: 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
Approach 3: Full Pyramid Pattern
C Program
#include <stdio.h>

int main()
{
    int n;
    int num;

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

    printf("Enter a number which is to be printed in pattern: ");
    scanf("%d", &num);
    for (int i = 0i < ni++)
    {
        for (int j = 0j < n - 1 - ij++)
        {
            printf(" ");
        }
        for (int j = n - 1 - ij <= n - 1 + ij++)
        {
            printf("%d"num);
        }
        printf("\n");
    }
    return 0;
}
Output:

Enter number of rows: 5 Enter a number which is to be printed in pattern: 4 4 444 44444 4444444 444444444
Approach 4: Full Inverted Pyramid Pattern
C Program
#include <stdio.h>

int main()
{
    int n;
    int num;

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

    printf("Enter a number which is to be printed in pattern: ");
    scanf("%d", &num);
    int space = 0;
    for (int i = 0i < ni++)
    {

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

Enter number of rows: 5 Enter a number which is to be printed in pattern: 4 444444444 4444444 44444 444 4


No comments:

Post a Comment