;

C Program To Print Diamond Pattern


Tutorialsrack 03/11/2019 Programs C

In this C program, we will learn how to write a program to print a diamond pattern using an asterisk (*).

Here is the code of the program to print a diamond pattern using an asterisk (*).

Code - C Program To Print Diamond Pattern
/*C program to print Diamond Pattern:
        *
       * *
      * * *
     * * * *
    * * * * *
    * * * * *
     * * * *
      * * *
       * *
        *
*/

#include<stdio.h>

#define MAX	5

int main()
{
	int i,j;
	int space=4;
	/*run loop (parent loop) till number of rows*/
	for(i=0;i< MAX;i++)
	{
		/*loop for initially space, before star printing*/
		for(j=0;j< space;j++)
		{
			printf(" ");
		}
		for(j=0;j<=i;j++)
		{
			printf("* ");
		}
		
		printf("\n");
		space--;
	}
	/*repeat it again*/
	space=0;
	/*run loop (parent loop) till number of rows*/
	for(i=MAX;i>0;i--)
	{
		/*loop for initially space, before star printing*/
		for(j=0;j< space;j++)
		{
			printf(" ");
		}
		for(j=0;j< i;j++)
		{
			printf("* ");
		}
		
		printf("\n");
		space++;
	}
    return 0;
}
Output

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

--------------------------------
Process exited after 0.0471 seconds with return value 0
Press any key to continue . . .


Related Posts



Comments

Recent Posts
Tags