Java Examples

java program to print floyd’s triangle

Table of Contents

FloydTriangle.java

import java.util.Scanner;
/**
 * @author Candid Java
 */

class FloydTriangle {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter the number of rows: ");
  int r = sc.nextInt(), n = 0;
  sc.close();
  for (int i = 0; i < r; i++) {
   for (int j = 0; j <= i; j++) {
    System.out.print(++n + " ");
   }
   System.out.println();
  }
 }
}

Output

Enter the number of rows: 5
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15