C Exercises: Display the pattern like a diamond
C For Loop: Exercise-31 with Solution
Write a program in C to display a pattern like a diamond.
The pattern is as follows :
* *** ***** ******* ********* ******* ***** *** *
Visual Presentation:

Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
int main() {
int i, j, r; // Declare variables for loop control and the number of rows.
// Prompt the user to input the number of rows.
printf("Input number of rows (half of the diamond): ");
scanf("%d", &r); // Read the number of rows from the user.
// Loop to print the upper half of the diamond.
for (i = 0; i <= r; i++) {
// Loop to print spaces before the asterisks.
for (j = 1; j <= r - i; j++)
printf(" ");
// Loop to print asterisks.
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n"); // Move to the next line after completing a row.
}
// Loop to print the lower half of the diamond.
for (i = r - 1; i >= 1; i--) {
// Loop to print spaces before the asterisks.
for (j = 1; j <= r - i; j++)
printf(" ");
// Loop to print asterisks.
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n"); // Move to the next line after completing a row.
}
return 0; // Return 0 to indicate successful execution.
}
Sample Output:
Input number of rows (half of the diamond) :5 * *** ***** ******* ********* ******* ***** *** *
Flowchart:

C Programming Code Editor:
Previous: Write a C program to find the Armstrong number for a given range of number.
Next: Write a C program to determine whether a given number is prime or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join