🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
A diamond pattern consists of two parts: an upper triangle and a lower inverted triangle. This pattern helps beginners in programming understand how to use nested loops for controlling spaces and printing characters, such as stars (*).
Problem Statement
Create a C program that:
- Accepts the number of rows (height of half the diamond).
- Prints a diamond-shaped pattern using stars (
*).
Example:
- Input:
rows = 5 - Output:
* *** ***** ******* ********* ******* ***** *** *
Solution Steps
- Input the Number of Rows: The size determines the height of the upper and lower parts of the diamond.
- Use Nested Loops: The first set of loops prints the upper triangle, and the second set prints the inverted triangle.
- Display the Diamond Pattern: Use loops to print stars and spaces to form the diamond shape.
C Program
#include <stdio.h>
int main() {
int i, j, rows;
// Step 1: Accept the number of rows (half the height of the diamond)
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Step 2: Print the upper part of the diamond (including the middle row)
for (i = 1; i <= rows; i++) {
// Step 3: Print spaces for alignment
for (j = i; j < rows; j++) {
printf(" ");
}
// Step 4: Print stars
for (j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
// Move to the next line after each row
printf("\n");
}
// Step 5: Print the lower part of the diamond (inverted triangle)
for (i = rows - 1; i >= 1; i--) {
// Step 6: Print spaces for alignment
for (j = rows; j > i; j--) {
printf(" ");
}
// Step 7: Print stars
for (j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
Explanation
Step 1: Input Number of Rows
- The program starts by accepting the number of rows from the user, which defines the height of the upper and lower parts of the diamond.
Step 2-4: Print the Upper Part of the Diamond
- The outer loop prints the upper part of the diamond, including the middle row.
- An inner loop prints spaces for alignment, and another inner loop prints the stars (
*). The number of stars increases as you move down the rows, forming the upper half of the diamond.
Step 5-7: Print the Lower Part of the Diamond
- The second part of the program prints the inverted triangle, which forms the lower part of the diamond.
- The number of spaces increases as you go down, while the number of stars decreases, creating the inverted effect.
Output Example
For rows = 5, the output will be:
*
***
*****
*******
*********
*******
*****
***
*
For rows = 6, the output will be:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Conclusion
This C program prints a diamond pattern using stars (*) by printing an upper triangle and an inverted lower triangle. The program effectively demonstrates the use of nested loops for formatting and aligning output, which is a useful exercise for beginners learning C programming.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment