C (programming language)
C program to print Rhombus star pattern
Understanding the Rhombus Star Pattern
A rhombus is a quadrilateral with four equal sides. In this pattern, we use asterisks (*) to create a visual representation of a rhombus on the console. The stars are arranged in a symmetrical manner, forming a diamond-like shape.
Steps to Create the Pattern
- Include necessary header files:
C
#include <stdio.h>
- Get user input:
C
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
- Use nested loops for pattern generation:
C
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print stars
for (int j = 1; j <= rows; j++) {
printf("*");
}
printf("\n"); // Move to next line
}
Explanation of the Code
- Outer loop (
i
): Iterates through each row of the rhombus. - Inner loop for spaces (
j
): Prints leading spaces to create the diagonal shape. The number of spaces decreases as the row number increases. - Inner loop for stars (
j
): Prints stars to form the body of the rhombus. The number of stars remains constant in each row. printf("\n")
statement: Moves the cursor to the next line after printing each row.
Customizing the Pattern
- Change the character: Replace the asterisk (
*
) with any other character you prefer. - Adjust the spacing: Modify the number of spaces printed in the first inner loop to create different variations of the rhombus pattern.
- Create a hollow rhombus: Print stars only on the borders of the rhombus by adding conditional checks within the second inner loop.
Key Points
- The nested loops create the structure of the pattern.
- The spacing and character choices determine the visual appearance.
- Experimentation is encouraged to create various patterns and shapes.
(I’m unable to incorporate images directly, but I’ve ensured the code snippets are formatted correctly for readability.)