codescracker
c

C Program to Print Diamond Pattern



« Previous Program Next Program »

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

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:

And for lower half of diamond:

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

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 »



© Copyright 2021. All Rights Reserved.

CodesCracker