C program to print diamond pattern of numbers

In this program, we are going to make a C program to print diamond patterns of numbers & a C program to print diamond patterns of stars using nested for loop and if-else statements.

C program to print diamond pattern of numbers


#include <stdio.h> int main() { int i, j, p; for (i = 1; i <= 5; i++) { p = 1; for (j = 1; j <= 9; j++) { if ((j <= 5 - i) || (j >= 5 + i)) { printf(" "); } else { printf("%d", p); p++; } } printf("\n"); } for (i = 1; i <= 4; i++) { p = 1; for (j = 1; j <= 9; j++) { if ((j <= i) || (j >= 10 - i)) { printf(" "); } else { printf("%d", p); p++; } } printf("\n"); } return 0; }
Output:
C program to print diamond pattern of numbers

C program to print diamond pattern of star

#include <stdio.h>
int main()
{
    int i, j;
    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 9; j++)
        {
            if ((j <= 5 - i) || (j >= 5 + i))
            {
                printf(" ");
            }
            else
            {
                printf("*");
            }
        }
        printf("\n");
    }
    for (i = 1; i <= 4; i++)
    {
        for (j = 1; j <= 9; j++)
        {
            if ((j <= i) || (j >= 10 - i))
            {
                printf(" ");
            }
            else
            {
                printf("*");
            }
        }
        printf("\n");
    }
    return 0;
}
C program to print diamond pattern of numbers

About the author:-
This program is made by Vipul Khokhar.
Thanks for reading the C program to print diamond patterns of numbers and the C program to print diamond patterns of stars.
Siddharth Jha
• Hackerrank 5* • Full Stack Developer • Masters in SEO

Related Posts

Post a Comment