You are here

Floyd's triangle in Java

Java program to display Floyd's triangle, in the triangle, there are n integers in the nth row and a total of (n(n+1))/2 integers in n rows. It is a simple pattern to print but helpful in learning how to create other patterns. The key to develop patterns is using nested loops appropriately.

Floyd's triangle Java program

import java.util.Scanner;
 
class FloydTriangle
{
  public static void main(String args[])
  {
    int n, num = 1, c, d;
    Scanner in = new Scanner(System.in);
 
    System.out.println("Enter the number of rows of Floyd's triangle to display");
    n = in.nextInt();
 
    System.out.println("Floyd's triangle:");
 
    for (c = 1; c <= n; c++)
    {
      for (d = 1; d <= c; d++)
      {
        System.out.print(num+" ");
        num++;
      }
 
      System.out.println(); // Moving to next row
    }
  }
}

Output of program:
Java Floyd's triangle program output

Download Floyd's triangle program class file.

Share this