* *** ***** ******* ********* ******* ***** *** *
Required knowledge:
Basic C programming, For loopLogic:
At single glance the patter seems difficult to be printed so, to make the pattern easy I have bisected the pattern in two halves. Where the first upper half looks like:* *** ***** ******* *********and the lower half looks like:
******* ***** *** *And if you notice these two patters are simple pyramid (with n rows) and reverse pyramid pattern (with n-1 rows). Hence, we just need to write down the codes of both pyramid and reverse pyramid star pattern one by one to get the final pattern.
Program:
/** * C program to print diamond star pattern */ #include <stdio.h> int main() { int i, j, n; printf("Enter value of n : "); scanf("%d", &n); //Prints the upper pyramid for(i=1; i<=n; i++) { for(j=i; j<n; j++) { printf(" "); } for(j=1; j<=(2*i-1); j++) { printf("*"); } printf("\n"); } //Prints the lower triangle for(i=n; i>=1; i--) { for(j=i; j<=n; j++) { printf(" "); } for(j=2; j<(2*i-1); j++) { printf("*"); } printf("\n"); } return 0; }
Output
Enter value of n : 5
*
***
*****
*******
*********
*******
*****
***
*
*
***
*****
*******
*********
*******
*****
***
*
Screenshot:
Happy coding ;)
You may also like
- All star patterns programs index.
- For loop programming exercises and solutions.
- If else programming exercises and solutions.
- C program to check whether a triangle is Equilateral, Scalene or Isosceles.
- C program to check whether a triangle is valid or not.
- C program to find GCD(HCF) of two numbers.
- C program to find LCM of two numbers.
- C program to find all factors of a given number.
- C program to check whether a number is Perfect number or not.
- C program to check whether a number is Strong number or not.
- C program to print Pascal triangle of n rows.
- C program to print fibonacci series.
- C program to check whether a number is Prime number or not.