C Program to Print Diamond Pattern

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

  • Diamond Pattern of Stars
  • Diamond Pattern of Numbers
  • Diamond Pattern of Alphabets

Print Diamond Pattern of Stars

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

#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 build and run under Code::Blocks IDE. Here is its output:

c program print diamond pattern

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

print diamond pattern of stars c

Program Explained

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

  • At first row, print 9 spaces, and one star
  • At second row, print 8 spaces, and three star
  • At third row, print 7 spaces, and five star
  • At fourth row, print 6 spaces, and seven star
  • At fifth row, print 5 spaces, and nine star
  • At sixth row, print 4 spaces, and eleven star
  • At seventh row, print 3 spaces, and thirteen star
  • At eighth row, print 2 spaces, and fifteen star
  • At ninth row, print 1 space, and seventeen star
  • At tenth row, print ninteen star

And for lower half of diamond:

  • At first row, print 1 space, and seventeen star
  • At second row, print 2 spaces, and fifteen star
  • At third row, print 3 spaces, and thirteen star
  • At fourth row, print 4 spaces, and eleven star
  • At fifth row, print 5 spaces, and nine star
  • At sixth row, print 6 spaces, and seven star
  • At seventh row, print 7 spaces, and five star
  • At eighth row, print 8 spaces, and three star
  • At ninth row, print 9 spaces, and one star

The dry run of above program goes like, supposing user input as 10:

  • 10 gets initialized to the variable row
  • Using the statement
    space = row-1;
    9 gets initialized to space
  • Now program flow executes the for loop
  • At first run of for loop with i=1, the dry run goes like:
    1. Inside the loop, 1 gets initialized to i and checks whether it is less than or equal to row or not
    2. The condition evaluates to be true, because obviously the value of i (1) is less than the value of row (10)
    3. Therefore program flow goes inside the loop's body and executes another for loop
    4. This time, 1 gets initialized to j and checks whether it is less than or equal to the value of space (9) or not
    5. The condition evaluates to be true
    6. Therefore program flow goes inside the loop body and executes only one statement, that is,
      printf(" ");
      that prints a space
    7. Now the variable j gets incremented and again program flow goes to the loop's condition part and checks whether the updated value or incremented value of j (2) is less than or equal to the value of space (9) or not
    8. The condition again evaluates to be true. Therefore program flow again goes inside the loop, and prints another space
    9. In this way printing of space continues until the value of j becomes greater than the value of space
    10. Therefore, total of 9 space gets printed at first row
    11. And then decrement the value of space to print one less space on next row (than previous one)
    12. Now the program flow goes to the second for loop part (present inside the first outer for loop)
    13. There, 1 gets initialized to j and checks whether it is less than or equal to the value of ((2*i)-1) or not
    14. Because the value of i was 1 at first run, therefore compares j with (2*i)-1 or (2*1)-1 or 1
    15. The condition evaluates to be true
    16. Therefore program flow goes inside the loop and prints a star (*)
    17. Then increments the value of j and compares again with ((2*i)-1)
    18. This time, the value of j (2) is not less than or equal to (2*i)-1, that is 1
    19. Therefore condition evaluates to be false, and the program flow exits the loop
    20. And using newline character (\n), program flow now starts the next output (printing) thing from new line
  • At second run of for loop with i=2, dry run goes like:
    • Using first for loop, prints 8 spaces. Process goes same from step no.1 to step no.10 as mentioned above with updated value of i
    • Again decrements the value of space to print one less space next time. Now space=8
    • Using second for loop, prints 3 stars. Process goes same from step no.12 to step no.19 as mentioned above with update value of i
    • Then process step no.20
  • At third run of for loop with i=3, dry run goes like:
    • Process the similar operation from step no.1 to step no.10 with new value of i (3). That is, prints 7 spaces
    • Decrements the value of space. Now space=7
    • Process the similar operation given from step no.12 to step no.19 with new value of i (3). That is, print 5 stars
    • Process step no.20
  • Continue executing the loop, until the value of i becomes 11
  • Because 11 is not less than or equal to row (10), therefore condition evaluates to be false, and the program flow exits from this loop
  • Process in similar way to print lower half of diamond
  • With initial value of space as 1
  • Because, at first row of lower half, we only have to print single space

Print Diamond Pattern of Numbers

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

Note - Always remember to initialize num with 1 after the statement printf("\n"); to start 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 above program assuming that user has entered 5 as the size of diamond:

print diamond pattern of numbers c

Print Diamond Pattern of Alphabets

This program is almost same as of previous one. In place of number, use a character using a variable say ch of char type. Initialize it with A. Rest of the things are similar with previous program.

#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 5:

diamond pattern of alphabets

Same Program in Other Languages

C Online Test


« Previous Program Next Program »