* ** *** **** ***** **** *** ** *If you are looking for reverse of this pattern find it here how to print mirrored half diamond star pattern.
Required knowledge:
Basic C programming, For loopLogic:
On first glance the pattern may look as a complex one to print. But if you look carefully you will find that the pattern can be divided into two parts to make our task easy. Let's us divide the pattern in two halves - the first upper half look likes:* ** *** **** *****and the second half should look like:
**** *** ** *If you have done previous stars patterns you will find that first pattern is the simple right triangle star pattern(with n rows) and second is the inverted right triangle star pattern(with n-1 rows). Now, we just need to write codes of both the program one by one to get the final pattern.
Program:
/** * C program to print half diamond star pattern series */ #include <stdio.h> int main() { int i, j, n; //Reads number of columns from user printf("Enter value of n : "); scanf("%d", &n); //Prints the upper half part of the pattern for(i=1; i<=n; i++) { for(j=1; j<=i; j++) { printf("*"); } printf("\n"); } //Prints the lower half part of the pattern for(i=n; i>=1; i--) { for(j=1; j<i; j++) { printf("*"); } printf("\n"); } return 0; }
Program to print half diamond star pattern
This logic was submitted by one of our Codeforwin reader Sankar Majumder. It uses two loops instead of four as above program./** * C program to print half diamond star pattern series. */ #include<stdio.h> int main() { int i, j, n, columns; // Read number of columns from user printf("Enter value of n:"); scanf("%d",&n); columns=1; for(i=1;i<=n*2;i++) { for(j=1; j<=columns; j++) { printf("*"); } if(i < n) { /* * Increment number of columns per row for upper part */ columns++; } else { /* * Decrement number of columns per row for lower part */ columns--; } 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.