C Program to Print a Diamond Pattern

In this article, you will learn and get code about the printing of a diamond pattern in the following ways:

Print a diamond pattern of stars

Let's create a program that asks the user to enter the row size of the upper-half diamond to print the diamond pattern of stars. For example, if the user enters 5 as the row size, then a diamond of stars of size 5*2-1 or 9 rows gets printed.

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, row, space;
    printf("Enter Number of Rows: ");
    scanf("%d", &row);
    space = row-1;
    for(i=1; i<=row; i++)
    {
        for(j=1; j<=space; j++)
            printf(" ");
        space--;
        for(j=1; j<=(2*i-1); j++)
            printf("*");
        printf("\n");
    }
    space = 1;
    for(i=1; i<=(row-1); i++)
    {
        for(j=1; j<=space; j++)
            printf(" ");
        space++;
        for(j=1; j<=(2*(row-i)-1); j++)
            printf("*");
        printf("\n");
    }
    getch();
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its output:

c program print diamond pattern

Now enter the number of rows, say 10, to print a diamond pattern that expands up to the row-1 line. That is, with row number 10, it will print a diamond pattern with 9 lines, as shown in the output given below:

print diamond pattern of stars c

Program Explained

If the user enters 10 as the size of the diamond, then always remember these things to print the upper half of the diamond:

And for the diamond's lower half:

The following is the dry run of the above program: suppose user input is 10.

Print a diamond pattern of numbers

Here is another program for printing diamond-shaped numbers. In each row, the number starts with 1. The program is similar to the previous one. Except, in place of the star, print the number using a variable named "num" (initialized with 1 at the start of the program). Increment the value of num after each print.

Remember to always initialize num with 1 after the statement printf("\n"); to begin with 1 for each row.

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, row, space, num=1;
    printf("Enter Number of Rows: ");
    scanf("%d", &row);
    space = row-1;
    for(i=1; i<=row; i++)
    {
        for(j=1; j<=(space); j++)
            printf(" ");
        space--;
        for(j=1; j<=(2*i-1); j++)
        {
            printf("%d", num);
            num++;
        }
        printf("\n");
        num=1;
    }
    space = 1;
    for(i=1; i<=(row-1); i++)
    {
        for(j=1; j<=(space); j++)
            printf(" ");
        space++;
        for(j=1; j<=(2*(row-i)-1); j++)
        {
            printf("%d", num);
            num++;
        }
        printf("\n");
        num=1;
    }
    getch();
    return 0;
}

Here is the output produced by the above program, assuming that the user has entered 5 as the size of the diamond:

print diamond pattern of numbers c

Print a Diamond Pattern of Alphabets

This program is nearly identical to the previous one. In place of a number, use a character using a variable, say, ch of the char type. Initialize it with A. The rest of the program is similar to the previous one.

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, row, space;
    char ch='A';
    printf("Enter Number of Rows: ");
    scanf("%d", &row);
    space = row-1;
    for(i=1; i<=row; i++)
    {
        for(j=1; j<=(space); j++)
            printf(" ");
        space--;
        for(j=1; j<=(2*i-1); j++)
        {
            printf("%c", ch);
            ch++;
        }
        ch='A';
        printf("\n");
    }
    space = 1;
    for(i=1; i<=(row-1); i++)
    {
        for(j=1; j<=(space); j++)
            printf(" ");
        space++;
        for(j=1; j<=(2*(row-i)-1); j++)
        {
            printf("%c", ch);
            ch++;
        }
        ch='A';
        printf("\n");
    }
    getch();
    return 0;
}

Here is its output, assuming the user input is 5:

diamond pattern of alphabets

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!