# Printing a Diamond Pattern in C

In this tutorial we are looking at how to print a diamond pattern using the C language. The diamond pattern algorithm question is often used in C courses, so it make sense that we also take a look at it.

Take a look at the diamond pattern C source code:

``````
#include <stdio.h>

int main() {

int rows, a, b, space;

printf("Enter number of rows:");
scanf("%d", &rows);
//scanf_s("%d", &rows, 1);

// Print first half of the triangle.
space = rows - 1;
for ( b = 1 ; b <= rows ; b++ ) {
for ( a = 1 ; a <= space ; a++ )
printf(" ");
space--;
for ( a = 1 ; a <= 2*b-1 ; a++)
printf("*");
printf("\n");
}

// Print second half of the triangle.
space = 1;
for ( b = 1 ; b <= rows - 1 ; b++ ) {
for ( a = 1 ; a <= space; a++)
printf(" ");
space++;
for ( a = 1 ; a <= 2*(rows-b)-1 ; a++ )
printf("*");
printf("\n");
}
return 0;
}

``````

First we ask for the number of rows we want the diamond half’s to be. As you can see we use scanf, but we put a scanf_s example in a comment-line for your convenience, because on windows scanf is deprecated, so you get a warning.

Then we print the top half of the diamond pattern, we are using three for loops for that. The first for loop keeps track of the number of rows. The second for loop is to set the spaces before the stars (that are set by the third for loop.

The second set of three for loops are used to print the second part of the diamond pattern. We end by returning zero.

That’s all, as you can see it is a diamond pattern program is easy to make. Play with the for loop settings to see what each loop does. Good luck!

This entry was posted in Programming Algorithms. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! or use to share this post with others.

### There are currently 9 responses to “Printing a Diamond Pattern in C”

Why not let us know what you think by adding your own comment!

1. Ajay Kumar on September 21st, 2012:

Really sir,,,,, u r awesome !!!!!!

2. vivith on February 18th, 2013:

thank you sir this is really help sir

3. david davis on April 17th, 2013:

I put in 36. it was a neat surprise. thanks.

4. vincent koech on May 31st, 2013:

you have really assist me.
regards

5. guna on June 30th, 2013:

really amazing

6. Vivek A on July 4th, 2013:

#include

#define NOR 5

int main()
{
char ucRow = 0;
char cStr = 0;
char cSpace = 0;

for(ucRow = -1 * (2*NOR – 2); ucRow <= (2*NOR – 2); ucRow+=2)
{
printf("\n");

for(cSpace = 0; cSpace < abs(ucRow) / 2; cSpace++)
printf(" ");

for(cStr = 0; cStr < ((2*NOR – 1) – abs(ucRow)); cStr++)
printf("*");
}

return !printf("\n");
}

Simplified code for printing diamond pattern. I just tried to reduce the number of loops…..

7. Mr Gee on July 21st, 2013:

Hi,

Just one question. Why give you a value to the variable space (space = 1) if you give to it another value 7 lines lower (space = rows – 1;) without using it with the original value?

8. admin on July 21st, 2013:

@Mr Gee: you are right, there is no reason for assigning 1 to the variable space. So I removed it. I think it was a leftover from when I wrote the example. Thanks for pointing it out!

9. Abdur Rehman Khalil on December 20th, 2013:

#include
int main()
{
int r = 7, a, b, c = 1, d, s;
for (s=r-1,b = 1; b <= r; b++)
{
for (a = 1; a <= s; a++) printf(" ");
if (c == 0) { s++; d = 2 * (r – b) – 1;}
else { s–; d = 2 * b – 1;}
for (a = 1; a <= d; a++) printf("*");
printf("\n");
if (b == r &&c == 1) { a = 1; s = 1; b = 0; c = 0;}
}
}