Floyd's Triangle

Floyd's Triangle

Easy C Pattern Printing 30 views
Explanation Complexity

Problem Statement

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 Consecutive numbers. Maintain the counter and continuously increase it.

Input Format

An integer n representing the number of rows.

Output Format

A number triangle with consecutive numbers printed row-wise.

Example

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

Constraints

• n ≥ 1

• Numbers must be continuous

Concept Explanation

Numbers are printed in a triangular form while maintaining a single counter that keeps increasing.

Step-by-Step Explanation

1.Initialize a variable count = 1.

2.Use an outer loop for rows from 1 to n.

3.For each row i:

4.Use an inner loop from 1 to i.

5.Print the current value of count.

6.Increment count by 1.

7.After the inner loop ends, move to the next line.

Concept Explanation

Numbers are printed in a triangular form while maintaining a single counter that keeps increasing.

Step-by-Step Explanation

1.Initialize a variable count = 1.

2.Use an outer loop for rows from 1 to n.

3.For each row i:

4.Use an inner loop from 1 to i.

5.Print the current value of count.

6.Increment count by 1.

7.After the inner loop ends, move to the next line.

Input / Output Format

Input Format
An integer n representing the number of rows.
Output Format
A number triangle with consecutive numbers printed row-wise.
Constraints
• n ≥ 1

• Numbers must be continuous

Examples

Input:
n = 5
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int rows = 5;     // Number of rows in the triangle
    int num = 1;      // Counter to keep track of consecutive numbers

    for (int i = 1; i <= rows; i++) {       // Loop for rows
        for (int j = 1; j <= i; j++) {      // Loop for columns in each row
            printf("%d ", num);
            num++;                           // Increase counter
        }
        printf("n");                        // Move to next row
    }

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int rows = 5;     // Number of rows in the triangle
    int num = 1;      // Counter to keep track of consecutive numbers

    for (int i = 1; i <= rows; i++) {       // Loop for rows
        for (int j = 1; j <= i; j++) {      // Loop for columns in each row
            printf("%d ", num);
            num++;                           // Increase counter
        }
        printf("n");                        // Move to next row
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.