C Program to print hollow diamond star pattern

Ex: Write a C Program to print hollow diamond star pattern. How to write a C Program to print hollow diamond star pattern. C Program to print hollow diamond star pattern.



We will print this pattern in two parts, first we print upper half of the pattern then we print below, half part of pattern.



  Step by step logic of the given program:


1. Accept input (number) from user declare variable say no.

2. print upper half of the pattern:

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

a). Run one outer for loop from 1 to no.

b). After that run inner loop from i to no to print inverted right triangle pattern.

c). Run another one inner loop from 1 to i*2-2  to print spaces.

d). Last run one more inner loop from i to no to print another inverted right triangle and to complete right side of upper half pattern.
Inside the outer loop, for new row, print new line (\n).


3. Print lower half of the pattern:

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

a). Run one outer for loop from 1 to no.

b). Inside the outer loop run inner loop from 1 to i to print  right triangle pattern.

c). Run another one inner loop from 1 to (no*2-i*2)  to print spaces.

d). Last run one more inner loop from 1 to i to print another right triangle and to complete right side of lower half pattern. 
Inside the outer loop, for new row, move to next line using \n.





Program:

#include<stdio.h>
void main()
{
int no;
int i,j;
printf("Enter the number:\n");
scanf("%d",&no);
/*loop to print upper half pattern*/
for(i=1;i<=no;i++)
{
for(j=i;j<=no;j++)
{                  printf("*");
}
for(j=1;j<=(i*2-2);j++)
{                                 
         printf(" ");
}
for(j=i;j<=no;j++)
{
         printf("*");
}
printf("\n");
}

/*loop to print lower half of the pattern*/
for(i=1;i<=no;i++)
{
for(j=1;j<=i;j++)
{                          
             printf("*");
}
for(j=1;j<=(no*2-i*2);j++)
{                                printf(" ");
}
for(j=1;j<=i;j++)
{
       printf("*");
}
printf("\n");
}
}


Above Program show's the following output:

C Program to print hollow diamond star pattern

No comments:

Post a Comment

If you have any doubts, please discuss here...👇