C Program to Print Floyd’s Triangle

Floyd’s Triangle is a triangular number pattern named after Robert Floyd. It is a right-angled triangle where the numbers are filled row by row. In this C program, we generate Floyd’s Triangle based on the user’s input for the number of rows.

Problem Statement

Given a positive integer ‘rows’, this program prints Floyd’s Triangle with ‘rows’ number of rows.

The program prompts the user to enter the number of rows they want in the Floyd’s Triangle. Then, it generates and prints the triangle using nested loops. Each row contains the numbers from 1 to ‘rows’ (incremented by 1 each time). Finally, the program terminates.

C Program to Print Floyd’s Triangle

#include <stdio.h>

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

    printf("Floyd's Triangle\n\n");

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    printf("\n");

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d ", num);
            num++;
        }
        printf("\n");
    }

    return 0;
}

How it works

  1. We start by including the necessary header file stdio.h for standard input/output operations.
  2. In the main function, we declare variables rows (to store the number of rows), i, j (loop counters), and num (to keep track of the numbers to be printed).
  3. We prompt the user to enter the number of rows they want in the Floyd’s Triangle.
  4. Using a nested for loop, we iterate over each row and column to print the numbers in the triangle.
  5. The outer loop i controls the number of rows, and the inner loop j prints the numbers in each row.
  6. Inside the inner loop, we print the value of num followed by a space, and then increment num by 1.
  7. After printing each row, we move to the next line using printf("\n").
  8. Once all the rows are printed, the program ends.

Input / Output

C Program to Print Floyd’s Triangle

Share:

Leave A Reply

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

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...