codescracker


c

C Program to Print Diamond Pattern of Stars



« Previous Program Next Program »


Print Diamond Pattern in C

To print diamond pattern of stars in C programming, you have to use six for loops, in which you have two outer for loops which contains two-two inner for loops to print Pyramid and Reverse Pyramid of stars to make diamond pattern of stars as shown in the following program.

C Programming Code to Print Diamond Pattern

Following C program ask to the user to enter the number of rows for diamond dimension to print the diamond pattern, then display the result on the screen:

/* C Program - Print Diamond Pattern */
		
#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int n, c, k, space=1;
    printf("Enter number of rows (for diamond dimension) : ");
    scanf("%d", &n);
    space=n-1;
    for (k=1; k<=n; k++)
    {
	for(c=1; c<=space; c++)
	{
		printf(" ");
	}
	space--;
	for(c=1; c<=(2*k-1); c++)
	{
		printf("*");
	}
	printf("\n");
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
	for(c=1; c<=space; c++)
	{
		printf(" ");
	}
	space++;
	for(c=1 ; c<=(2*(n-k)-1); c++)
	{
		printf("*");
	}
	printf("\n");
    }
    getch();
}

When the above c program is compile and executed, it will produce the following result:

c program print diamond pattern

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test