🎓 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 palindrome pyramid pattern consists of numbers arranged in such a way that they form a mirrored sequence, making each row a palindrome. The numbers increase from 1 to the current row number, and then decrease back to 1. This exercise helps in practicing nested loops and number manipulation.
Problem Statement
Create a C program that:
- Accepts the number of rows for the pyramid.
- Prints a palindrome pyramid pattern.
Example:
- Input:
rows = 5 - Output:
1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1
Solution Steps
- Input the Number of Rows: The user defines how many rows the pyramid should have.
- Use Nested Loops: The outer loop handles the rows, and the inner loops handle printing spaces and the palindrome numbers.
- Display the Palindrome Pyramid: Print numbers increasing from
1up to the row number and then decrease back to1.
C Program
#include <stdio.h>
int main() {
int i, j, rows;
// Step 1: Accept the number of rows for the pyramid
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Step 2: Outer loop for rows
for (i = 1; i <= rows; i++) {
// Step 3: Print spaces for alignment
for (j = i; j < rows; j++) {
printf(" ");
}
// Step 4: Print increasing numbers
for (j = 1; j <= i; j++) {
printf("%d", j);
}
// Step 5: Print decreasing numbers
for (j = i - 1; j >= 1; j--) {
printf("%d", j);
}
// Move to the next line after printing each row
printf("\n");
}
return 0;
}
Explanation
Step 1: Input Number of Rows
- The program begins by asking the user for the number of rows for the palindrome pyramid.
Step 2: Outer Loop for Rows
- The outer loop controls how many rows are printed, running from
1torows.
Step 3: Print Spaces for Alignment
- The inner loop prints spaces before the numbers to align the pyramid in the center.
Step 4: Print Increasing Numbers
- The second inner loop prints numbers in increasing order from
1to the current row number (i).
Step 5: Print Decreasing Numbers
- The third inner loop prints numbers in decreasing order starting from
i - 1down to1.
Output Example
For rows = 5, the output will be:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
For rows = 4, the output will be:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Conclusion
This C program prints a palindrome pyramid pattern by using nested loops to print numbers in increasing and decreasing order. The spaces before the numbers ensure proper alignment to create the pyramid shape. This exercise helps in practicing loop control, number manipulation, and formatting output in 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