Loop Assignment 20 — Print Floyd's Triangle Using Nested For Loops
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Print Floyd's Triangle Using Nested For Loops — Consecutive Numbers in a Triangle

📑 On this page:
  • How Floyd's Triangle Works
  • The Three Steps of Printing the Triangle
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions
📚 In this program, you will learn:
  • How to print Floyd's triangle using nested for loops
  • How to print consecutive natural numbers in a triangular pattern
  • How the outer loop controls rows and the inner loop controls columns
  • How to maintain a running number across all rows
  • How to format the output using spaces
  • Why Floyd's triangle is a common nested-loop practice problem

How Floyd's Triangle Works

Floyd's triangle is a right-angled triangular pattern made using consecutive natural numbers. It starts with 1, and every following number is one greater than the previous number.

The first row contains one number, the second row contains two numbers, the third row contains three numbers, and so on. For example, a Floyd's triangle with 5 rows looks like this:

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

The program uses two loops: an outer loop for rows and an inner loop for columns. A separate variable keeps track of the next number to print, so the numbering continues smoothly from one row to the next.

💡 In one line: Use an outer loop for rows, an inner loop for columns, and print an increasing number after every position.

Diagram showing Floyd's triangle printed using nested for loops in C

The Three Steps of Printing the Triangle

Step Loop operation Effect
1. Control the rowsfor (i = 1; i <= rows; i++)Creates one row in each iteration
2. Print the numbersfor (j = 1; j <= i; j++)Prints exactly i numbers in row i
3. Increase the numbernumber++;Ensures the next number is always one greater

The number variable must be declared outside both loops. If it is declared inside the inner loop, it will reset for every column and the triangle will not contain consecutive numbers.

Floyd's triangle is useful for practicing nested loops because the number of values in each row depends on the row number. Row 1 has 1 value, row 2 has 2 values, row 3 has 3 values, and so on.

Complete C Program

Program: print Floyd's triangle using nested for loops

#include <stdio.h>

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

    printf("Enter the number of rows: ");
    if (scanf("%d", &rows) != 1 || rows <= 0) {
        printf("Invalid input. Please enter a positive integer.\n");
        return 1;
    }

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

    return 0;
}
                                    

Program Output

Enter the number of rows: 5

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

A second run, with 4 rows:

Enter the number of rows: 4

1 
2 3 
4 5 6 
7 8 9 10 
                                

The program prints consecutive natural numbers from 1 onward. Row 1 contains one number, row 2 contains two numbers, row 3 contains three numbers, and row 4 contains four numbers.

Step-by-Step Explanation

Let us understand how the program prints Floyd's triangle for 5 rows.

1. Initial value

The variable number is initialized to 1. This is the first number to be printed. The variable rows stores the total number of rows entered by the user.

2. First row

When i = 1, the inner loop runs once. It prints 1 and increases number to 2. Then the program prints a newline.

3. Second row

When i = 2, the inner loop runs twice. It prints 2 and 3. After each print, number is increased. Then the program prints a newline.

4. Third row

When i = 3, the inner loop runs three times. It prints 4, 5, and 6. Then the program prints a newline.

5. Fourth and fifth rows

When i = 4, the program prints 7 8 9 10. When i = 5, it prints 11 12 13 14 15. The numbering continues without restarting.

6. Why the inner loop runs i times

The condition j <= i makes the inner loop run exactly i times. Therefore, row 1 prints one number, row 2 prints two numbers, row 3 prints three numbers, and so on.

7. Why number is declared outside the loops

The variable number must remain unchanged between rows. Since it is declared before the loops, its value continues from the last printed number into the next row.

8. Why a newline is printed after the inner loop

The statement printf("\n"); is placed after the inner loop but inside the outer loop. This moves the output to the next line after each row is completed.

9. Total numbers printed

For n rows, Floyd's triangle contains \(1 + 2 + 3 + \dots + n\) numbers. This total is calculated using the formula \(n(n + 1) / 2\). For 5 rows, the total number of values is 15.

10. Complexity summary

Time complexity is O(n2), because the program prints 1 number in the first row, 2 numbers in the second row, and so on. Space complexity is O(1), because the program uses only a few integer variables and does not store the triangle in an array.

🎯 Remember this: Use an outer loop for rows, an inner loop for columns, and keep a running variable starting from 1. Print it, increase it, and continue until all rows are complete.

Frequently Asked Questions

1. What is Floyd's triangle?

Floyd's triangle is a right-angled triangular pattern of consecutive natural numbers. It starts with 1, and each following number increases by 1.

2. Why are nested loops used?

Nested loops are used because the program must handle both rows and columns. The outer loop controls the row number, while the inner loop prints the required number of values in that row.

3. Why does the inner loop run i times?

Row i contains exactly i numbers. Therefore, the inner loop condition j <= i ensures that the correct number of values is printed in each row.

4. What happens if number is declared inside the inner loop?

If number is declared inside the inner loop, it will be reinitialized every time the inner loop runs. The output will not contain consecutive numbers, so the triangle will be incorrect.

5. How many numbers are printed in an n-row Floyd's triangle?

An n-row Floyd's triangle contains \(n(n + 1) / 2\) numbers. For example, a 5-row triangle contains 15 numbers.

6. What is the time complexity of this program?

The time complexity is O(n2), because the total number of printed values is proportional to \(n(n + 1) / 2\). Space complexity is O(1), since no array is used.

💡 Tip: After implementing this program, try printing a Floyd's triangle using only odd numbers, even numbers, or alphabets. The loop structure remains the same; only the value being printed changes.

📖 Related Tutorials

  • 19. Print Pascal's Triangle Using Nested For Loops
  • 21. Print an X-Shaped Star Pattern Using Nested For Loops
  • 9. Print the Pattern of a Pyramid Using a For Loop
  • 19. Print the Pattern of a Right Angled Triangle Using a For Loop
  • All Loop Assignments in C

Previous Topic: -->> 19. Write a program in C to print Pascal's triangle using nested for loops.   ||   Next Topic: -->> 21. Write a program in C to print an X-shaped star pattern using nested for loops.


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.