0% found this document useful (0 votes)
262 views

Diamond Pattern

The document describes a C program to print a diamond star pattern of variable size. It explains the logic and steps to print increasing then decreasing numbers of stars and spaces on each line to create the shape. The program takes the number of rows as input, uses loops and conditional logic to increment and decrement the stars and spaces printed on each line, and outputs the diamond pattern.

Uploaded by

Shanmuk_Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
262 views

Diamond Pattern

The document describes a C program to print a diamond star pattern of variable size. It explains the logic and steps to print increasing then decreasing numbers of stars and spaces on each line to create the shape. The program takes the number of rows as input, uses loops and conditional logic to increment and decrement the stars and spaces printed on each line, and outputs the diamond pattern.

Uploaded by

Shanmuk_Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a C program to print diamond star pattern series using for loop.

How to print
diamond star pattern structure in C program. Logic to print diamond star pattern
series in C programming.

Example

Input

Input rows: 5
Output

*
***
*****
*******
*********
*******
*****
***
*

Required knowledge
Basic C programming, If else, For loop, Nested loop

Logic to print diamond star pattern


*
***
*****
*******
*********
*******
*****
***
*

The given pattern is a combination of simple pyramid star pattern and inverted
pyramid star pattern. It consist of N*2-1 rows (for this case N=5). Each row
contain spaces and stars in printed in increasing and decreasing order.

Stars are printed in increasing order till Nth row. After Nth row stars are printed
in decreasing order.
Spaces are printed in decreasing order till Nth row. After Nth row spaces are
printed in increasing order. Point your mouse cursor over the pattern to count
total spaces.

Step by step descriptive logic to print diamond star pattern.

Input number of rows to print from user (in real number of rows/2). Store it in a
variable say rows.
Declare two variables to keep track of total columns to print each row, say stars=1
and spaces=N-1.
To iterate through rows, run an outer loop from 1 to rows*2-1. The loop structure
should look like for(i=1; i<rows*2; i++).
To print spaces, run an inner loop from 1 to spaces. The loop structure should look
like for(j=1; j<=spaces; j++). Inside this loop print single space.
To print stars, run another inner loop from 1 to stars*2-1. The loop structure
should look like for(j=1; j<=stars; j++). Inside this loop print star.
After printing all columns of a row, move to next line i.e. print new line.
Check if(i < rows) then increment stars and decrement spaces. Otherwise increment
spaces and decrement stars.
Program to print diamond star pattern
/**
* C program to print diamond star pattern
*/

#include <stdio.h>

int main()
{
int i, j, rows;
int stars, spaces;

printf("Enter rows to print : ");


scanf("%d", &rows);

stars = 1;
spaces = rows - 1;

/* Iterate through rows */


for(i=1; i<rows*2; i++)
{
/* Print spaces */
for(j=1; j<=spaces; j++)
printf(" ");

/* Print stars */
for(j=1; j<stars*2; j++)
printf("*");

/* Move to next line */


printf("\n");

if(i<rows)
{
spaces--;
stars++;
}
else
{
spaces++;
stars--;
}
}

return 0;
}
Output
Enter N: 5
*
***
*****
*******
*********
*******
*****
***
*

You might also like