Diamond pattern program in c language

Diamond pattern program in c language with the video explanation.

Social Shares
  • 1
  •  
  •  
  • 1
  •  
  •  
  •  
  •  

Diamond pattern program in c language

Hello readers, today you are going to learn how to print diamond pattern program in c language. Before start print diamond pattern we need to know how looping statement works because looping statements like for loop or while loop plays a very important role to make a pattern program in c. If you have good knowledge of looping statements in c language then you can go with this session. So let’s get started.

Watch video of diamond pattern program in c

Explanation of  diamond pattern program in c

As you know first of all we include the library files of c programming in the program. so I included two header files named <stdio.h> and <conio.h> respectively with the help of include keyword.

#include<stdio.h>
#include<conio.h>

Then I started the ‘main function‘ which is necessary to add in every program because without ‘main function’  our program will not be executed by the compiler and compiler will show you an error.

void main()
{
}

After that, we declared five integer type variables i, j, space, rows and last but not leastwhich is initialized by zero. We declared i & j variable for loop counter which counts the number of time the loop is executed. A space variable represents the number of space in the diamond pattern program in c language. The row variable represents the number of rows in the diamond pattern program in c.


int i, j, space, rows, k=0;

Now we use ‘printf function’ to show the message of ‘Enter number of rows’ to the user. After that, we store that value with the help of ‘scanf function‘ which was entered by the end-user.


printf("Enter the number of rows");
scanf("%d",&rows);

After that, I start an outer ‘for loop’ 


for(i=1;i<=rows;++i,k=0)

In that loop, I initialized i by 1 and then provide the condition to the loop which is i<=rows. This loop executes until the condition gets false. for example, rows variable has 4 value than the loop will execute four times and every execution of loop the counter variable increase itself by one.

After declaring the outer loop, I also declare an inner for loop.


for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}

In this loop, the space variable initialized by 1. The loop executes until condition (space<=rows-1) get false. The loop will execute always one lesser to the number of rows which was given by the end user. The space variable always incrementing itself by one. In inner for loop has multiple statements as you can see. There is a printf function in C programming language which prints the space on the screen and to print stars I use a while loop inside the inner for loop. In while loop, I provide k!=2*i-1 as a condition. The value of k will always depend on the value of i. That condition means k is not equal to two times minus one if that condition is true then while loop will print stars on the screen.

Till here we have achieved our half goal which means now we can print half diamond or you can say that a pyramid. Now do the left work because we want to print diamond pattern program in c.


for(i=rows-1; i>=1; --i)
{
for(space=0; space < rows-i; ++space)
printf(" ");
for(j=i; j <= 2*i-1; ++j)
printf("* ");
for(j=0; j < i-1; ++j)
printf("* ");
printf("\n");
}

As you can see above code there are 3 inner for loops in an outer for loop. Let’s take outer for loop. In outer for loop, i is initialized by  rows-1. There is a condition as i>=1 which means the value of i should grater equal to one. If that condition will not match then the loop will terminate.

Now come on to the first inner for loop.

In that loop space variable is initialized by zero and loop will execute until the condition (space<rows-1) gets false. If that condition is true then the loop print spaces on the screen.

The second and third inner for loops will print stars on the screen. Both loops execute their blocks until the condition gets false and they incrementing by one.

Congratulations! now you have achieved your goal to print diamond pattern program in c language.

A result image of diamond pattern program in c

Diamond pattern program in c language
Result image of diamond pattern program in c language

Source Code of diamond pattern program in c

 //Program to print diamond pattern
#include <stdio.h> //including the library files.
#include <conio.h>
void main() //start the main function.
{
int i, j, space, rows, k=0; //declaring the variables.
clrscr();
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
for(i=rows-1; i>=1; --i)
{
for(space=0; space < rows-i; ++space)
printf(" ");
for(j=i; j <= 2*i-1; ++j)
printf("* ");
for(j=0; j < i-1; ++j)
printf("* ");
printf("\n");
}
getch();
}

I hope you understand how to print diamond pattern program in c. If you understand then please share my hard work with your friends. Subscribe my newsletter for upcoming article’s updates direct in your mailbox. If you have any confusion or query then you can leave a message on my facebook page. At last, I would like to say please don’t forget to subscribe to my youtube channel. Thank you for visiting.


Social Shares
  • 1
  •  
  •  
  • 1
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *