Inverted Half Diamond Star Pattern
Program to Print Inverted Half Diamond Star Pattern
PRINTING PATTERN:
* ** *** **** *** ** *
PREREQUISITE:
Basic knowledge of C language and loops.
ALGORITHM:
1. Take the number of rows as input from the user (height of the upper half of the inverted diamond) and store it in a variable ('n').
2. Run a loop 'n' times to print the upper half of the inverted diamond (i=1 to i<=n).
3. For each row, print spaces for alignment and then print stars equal to the current row number.
4. Run another loop 'n-1' times to print the lower half of the inverted diamond (i=n-1 to i>0).
5. For each row, print spaces for alignment and then print stars equal to the current row number.
6. Move to the next line after each row has been printed.
Code in C:
#include <stdio.h> int main() { int n, i, j; // Declaring integers for rows and loop counters printf("Enter the number of rows for the inverted half diamond:\n"); scanf("%d", &n); // Taking input for number of rows // Upper half of the inverted diamond for (i = 1; i <= n; i++) { // Print spaces for alignment for (j = n - i; j > 0; j--) { printf(" "); // Printing spaces } // Print stars for each row for (j = 1; j <= i; j++) { printf("*"); // Printing stars } printf("\n"); // New line after each row } // Lower half of the inverted diamond for (i = n - 1; i > 0; i--) { // Print spaces for alignment for (j = n - i; j > 0; j--) { printf(" "); // Printing spaces } // Print stars for each row for (j = 1; j <= i; j++) { printf("*"); // Printing stars } printf("\n"); // New line after each row } return 0; }
Output:
Enter the number of rows for the inverted half diamond: 4 * ** *** **** *** ** *