#include <stdio.h>

int main() {
    int rows, i, j, number = 1;

    // Asking user to input the number of rows
    printf("Enter the number of rows for Floyd's Triangle: ");
    scanf("%d", &rows);

    // Loop to iterate over each row
    for (i = 1; i <= rows; i++) {
        // Loop to print the numbers in each row
        for (j = 1; j <= i; j++) {
            printf("%d ", number);  // Print the current number
            number++;  // Increment the number to be printed next
        }
        printf("\n");  // Move to the next line after printing each row
    }

    return 0;
}

Explanation:

Variables:

rows: Holds the number of rows for Floyd’s Triangle (user input).

i, j: Loop counters for rows and elements in each row.

number: Starts at 1 and increments to print numbers in sequence.

User Input:

The program asks the user to enter the number of rows.

Loops:

Outer loop (i): Controls the number of rows.

Inner loop (j): Prints numbers in each row. The count of numbers increases with each row.

Number Printing:

The number is printed, and it increments after every print to maintain sequence.

A new line is added after each row to shape the triangle.

Output:

The program prints Floyd’s Triangle based on the number of rows entered by the user.

Output:

Enter the number of rows for Floyd's Triangle: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights