🎓 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
Floyd's Triangle is a triangular array of natural numbers, where each row contains an increasing sequence of numbers starting from 1. It is a common exercise in programming that helps you practice loops and number sequences.
Problem Statement
Create a C program that:
- Accepts the number of rows for Floyd's Triangle.
- Prints Floyd's Triangle with natural numbers.
Example:
- Input:
rows = 5 - Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Solution Steps
- Input the Number of Rows: The size determines how many rows Floyd’s Triangle will have.
- Use Nested Loops: The outer loop will handle the rows, while the inner loop will handle printing the numbers.
- Display Floyd's Triangle: Print the numbers in increasing order, row by row.
C Program
#include <stdio.h>
int main() {
int i, j, rows, num = 1;
// Step 1: Accept the number of rows for Floyd's Triangle
printf("Enter the number of rows: ");
scanf("%d", &rows);
// Step 2: Outer loop for the rows
for (i = 1; i <= rows; i++) {
// Step 3: Inner loop to print numbers
for (j = 1; j <= i; j++) {
printf("%d ", num);
num++; // Increment the number after printing
}
// Move to the next line after each row
printf("\n");
}
return 0;
}
Explanation
Step 1: Input Number of Rows
- The program begins by asking the user to input the number of rows for Floyd’s Triangle.
Step 2: Outer Loop for Rows
- The outer loop controls how many rows to print. It runs from 1 up to the number of rows specified by the user.
Step 3: Inner Loop for Numbers
- The inner loop prints the numbers for each row. The number of values printed increases as the row number increases. The
numvariable is used to keep track of the current number to print and is incremented after each print.
Output Example
For rows = 5, the output will be:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
For rows = 6, the output will be:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Conclusion
This C program prints Floyd’s Triangle by incrementing and displaying numbers in increasing order row by row. The exercise is helpful in practicing nested loops and number sequences 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