Floyd’s Triangle in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

void floyd(int n) {
    int k = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            printf("%d ", k++);
        printf("\n");
    }
}

C Output

Input:  
5  

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


C++ Program

void floyd(int n) {
    int k = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            cout << k++ << " ";
        cout << "\n";
    }
}

C++ Output

Input:  
4  

Output:  
1  
2 3  
4 5 6  
7 8 9 10


JAVA Program

void floyd(int n) {
    int k = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            System.out.print(k++ + " ");
        System.out.println();
    }
}

JAVA Output

Input:  
3 

Output:  
1  
2 3  
4 5 6


Python Program

def floyd(n):
    k = 1
    for i in range(1, n + 1):
        for j in range(i):
            print(k, end=" ")
            k += 1
        print()

Python Output

Input:  
5  

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


In-Depth Explanation
Example
Suppose the input is n = 4. So, the triangle will have 4 rows. We begin with the digit 1 and continue printing one by one:


Row 1: 1  
Row 2: 2 3  
Row 3: 4 5 6  
Row 4: 7 8 9 10
Every row prints one more digit than the previous one, and all digits are natural numbers arranged in increasing order.

Real-Life Analogy
Suppose you're numbering seats in a triangular room:
Row 1 contains 1 seat, Row 2 contains 2, Row 3 contains 3, and so on. You begin at seat number 1 and number seats consecutively row by row. That's just what Floyd's Triangle does — it arranges numbers in an increasing, triangular pattern.

This is also analogous to how pyramid levels are completed in stack games or individuals are aligned in formation in sports or in military training — adding layer by layer.

Why It Matters
Floyd's Triangle assists novices in learning:

Nested loops

Sequential numbering

Pattern construction with dynamic boundaries

It explains how inner and outer loops operate with the outer loop governing rows and the inner loop regulating the number of columns in a row. This methodology is applied in triangle-based pattern questions and matrix printing in competitive programming.

What You Learn from This
You learn:

The value of employing a counter (k) that persists across loops

Nested loop design based on row number

Dynamic loop limits (such as printing i numbers in the i-th row)

Floyd's Triangle is an excellent starter problem to develop looping arithmetic, number patterns, and 2D-like output formatting.

Interview Relevance and Real Projects
This problem is well-liked in:

Programming aptitude rounds

School/college-level coding exams

Competitive programming pattern-building questions

It's applied to assess:

Comprehension of nested loops

Control over series of numbers

Ability to format triangular patterns

In real-world usage, the same logic applies to:

Data visualizations (triangle/pyramid shaped)

Index. structures (such as triangle storage in RAM)

Number theory education tools and visualization software

SEO-Optimized Description
Floyd's Triangle is an old problem-solving pattern that allows novices to see how numbers are printed in a well-organized, triangular format using loops within loops. In C, C++, Java, or Python, Floyd's Triangle begins at 1 and prints numbers row by row where every row has one more number than the last. This exercise is instructional in loop control, sequential numbering, and triangle pattern logic and is therefore suitable for interviews, homework at school, and competitive programming practice. Learning Floyd's Triangle enhances your level of understanding on how to use nested loops and pattern construction, which underlies most matrix and graphic-related problems in programming.