C Program
#include <stdio.h> int main(){ int n=5,k=1; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++) printf("%d ",k++); printf("\n"); } }
C Output
Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
C++ Program
#include <iostream> using namespace std; int main(){ int n=4,k=1; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++) cout<<k++<<" "; cout<<"\n"; } }
C++ Output
Input: n = 4 Output: 1 2 3 4 5 6 7 8 9 10
JAVA Program
public class Main{ public static void main(String[] args){ int n=6,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: n = 6 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Python Program
n=3;k=1 for i in range(1,n+1): for j in range(i): print(k,end=" ");k+=1 print()
Python Output
Input: n = 3 Output: 1 2 3 4 5 6
Deep-Dive Explanation
What is Floyd's Triangle?
Floyd's Triangle is a basic mathematical sequence that places consecutive natural numbers in a triangular shape. It's named after Robert Floyd, who employed it to demonstrate algorithms.
Step-by-Step Logic
Begin with a counter k = 1.
Outer loop iterates n rows.
Inner loop iterates i times (same as the row number).
Print k and increment k after every print.
Go to the next line after the end of each row.
Example for n = 5
Row 1 → 1
Row 2 → 2 3
Row 3 → 4 5 6
Row 4 → 7 8 9 10
Row 5 → 11 12 13 14 15
Why It's Useful in Learning
Teaches nested loops
Reinforces loop control and incrementing variables
Helps in pattern printing, which is common in interviews
Encourages mathematical thinking
Real-Life Analogy
Pretend to have a seating plan in a triangular shape where every seat is numbered consecutively from front to rear.
Uses
Number placement questions
Memory representation of triangular arrays
Pattern creation in the printing program
Interview Tip
Interviewers sometimes even ask you to create Floyd's Triangle in reverse, or starting from a number, which is an extension of this simple version.
Social Plugin