Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

Write a Java program to print Floyd’s triangle?

Floyd's triangle is a right-angled triangular pattern arranged in rows of numbers from 1 to n. Using Nested loops, you can create Floyd's triangle with Java that consists of two loops: an outer loop that controls rows and an inner loop that controls columns.

Each number in Floyd's triangle equals the sum of the two numbers above it.


1

2 3

4 5 6

7 8 9 10

Here is the Java program that prints Floyd’s triangle.


import java.util.Scanner;
 
 class FloydTriangle
 {
    public static void main(String args[])
    {
       int num1, num2 = 1, i, j;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number of rows for Floyd's Triangle");
       num1 = sc.nextInt();
       System.out.println("Floyd's Triangle:");
       for ( i = 1; i <= num1; i++ )
       {
          for ( j = 1; j <= 1; j++ )
            {
               System.out.print(num2+" ");
               num2++;
            }
           System.out.println();
       }
    }
 }
 

Output:


Enter the number of rows for Floyd's Triangle
5


Floyd's Triangle:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Test your website on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free