Print a Floyd’s Triangle Pattern in C

To print a Floyd’s Triangle pattern in C, we use nested loops to generate a right-angled triangular number pattern. The Floyd’s Triangle consists of consecutive numbers starting from 1 and increasing row-wise. Each row contains one more element than the previous row.


Examples to Print Floyd’s Triangle

1. Basic Floyd’s Triangle

In this example, we will print a Floyd’s Triangle with a fixed number of rows. The numbers will be arranged in a right-angled triangle pattern, starting from 1 and increasing sequentially.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5, num = 1;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Inner loop for columns
        for (int j = 1; j <= i; j++) {
            printf("%d ", num);
            num++; // Increment number
        }
        printf("\n"); // Move to next line after each row
    }

    return 0;
}

Explanation:

  1. We declare an integer variable rows with a value of 5 to define the number of rows in the triangle.
  2. We initialize num to 1, which represents the starting number in the Floyd’s Triangle.
  3. The outer for loop runs from i = 1 to rows to control the number of rows.
  4. The inner for loop runs from j = 1 to i to control the number of columns in each row.
  5. Inside the inner loop, we print num and increment it after each print operation.
  6. After completing a row, we print a newline character to move to the next row.

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

2. Floyd’s Triangle with User Input

In this example, we will allow the user to input the number of rows for the Floyd’s Triangle.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows, num = 1;

    // Taking user input for number of rows
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Inner loop for columns
        for (int j = 1; j <= i; j++) {
            printf("%d ", num);
            num++; // Increment number
        }
        printf("\n"); // Move to next line
    }

    return 0;
}

Explanation:

  1. We declare an integer variable rows to store the number of rows entered by the user.
  2. We initialize num to 1, the first number in the sequence.
  3. We prompt the user to enter the number of rows using printf() and scanf().
  4. The nested loops generate the Floyd’s Triangle in the same way as the previous example.

Example Output:

Enter the number of rows: 4
1
2 3
4 5 6
7 8 9 10

3. Floyd’s Triangle with Even Numbers

In this example, we will modify the Floyd’s Triangle to display only even numbers.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5, num = 2;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Inner loop for columns
        for (int j = 1; j <= i; j++) {
            printf("%d ", num);
            num += 2; // Increment by 2 to get the next even number
        }
        printf("\n"); // Move to next line
    }

    return 0;
}

Explanation:

  1. We initialize num to 2 to start with the first even number.
  2. The loops function in the same way as before, but now num is incremented by 2 each time to ensure even numbers are printed.

Output:

2
4 6
8 10 12
14 16 18 20
22 24 26 28 30

Conclusion

In this tutorial, we learned different ways to print Floyd’s Triangle in C:

  1. Basic Floyd’s Triangle with fixed rows.
  2. User-input-based Floyd’s Triangle.
  3. Modified Floyd’s Triangle with even numbers.