Java program to print Floyd’s triangle

Let’s learn java program to print Floyd’s triangle.

Java program to print Floyd’s triangle

How to print floyd’s triangle in java with example is one of the common java interview question. First let’s learn what is floyd’s triangle in java.

Floyd triangle is a right angled triangular array of natural numbers starting with 1 in the top left corner.

java program to print floyd's triangle

In floyd triangle there are total (n(n + 1)) / 2 integers where “n” is integers in nth row.

Floyd’s triangle pattern looks like this,

1
2 3
4 5 6
7 8 9 10

Let’s learn how to print floyd’s triangle in java program. So in printing floyd’s triangle in java first user enters number of rows.

To read user input we are using nextInt() method of Scanner class. Once you have number of rows it is very easy to print floyd’s triangle.

If you notice below java program output, floyd’s triangle will look like two dimensional array or a table.

Also read – gcd and lcm of two numbers

Obviously we need two for loops. First for loop print number of rows and second for loop print numbers in each row.

Here we need to keep an integer outside for loop and increase it on inner for loop. This doesn’t make rows to reset in-between.

Now let’s see java program to print floyd’s triangle.

 /*
 * Java program to print floyd's triangle:
 * 1
 * 2 3
 * 4 5 6
 * 7 8 9 10
 */
import java.util.Scanner;
public class PrintFloydsTriangle
{
   public static void main(String[] args)
   {
      int x, y, z, number = 1;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the number of rows of floyd's triangle you want to print: ");
      x = sc.nextInt();
      System.out.println("Floyd's Triangle: ");
      for(y = 1; y <= x; y++)
      {
         for(z = 1; z <= y; z++)
         {
            System.out.println(number + " ");
            number++;
         }
         System.out.println();
      }
      sc.close();
   }
}


Output:

Enter the number of rows of floyd’s triangle you want to print: 4
Floyd’s Triangle:

1
2 3
4 5 6
7 8 9 10

Enter the number of rows of floyd’s triangle you want to print: 5
Floyd’s Triangle:

1

2 3

4 5 6

7 8 9 10

11 12 13 14 15


Inverted floyd triangle in java

Now let’s learn inverted floyd triangle in java. Here’s the java program,

/*
* Java program to print inverted floyd's triangle in java:
*  10 9 8 7 
*  6 5 4 
*  3 2 
*  1 
*/
import java.util.Scanner;
public class FloydTriangleDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      // getting number of rows from user
      System.out.println("Enter the number of rows of floyd's triangle you want to print: ");
      int rows = sc.nextInt();
      int number = rows * (rows + 1) / 2;
      System.out.println("** Inverted floyd triangle **");
      for(int a = rows; a >= 1; a--)
      {
         for(int b = 1; b <= a; b++)
         {
            System.out.println(number + " ");
            number = number - 1;
         }
         System.out.println();
      }
      sc.close();
   }
}


Output:

Enter the number of rows of floyd’s triangle you want to print: 4
** Inverted floyd triangle **
10 9 8 7
6 5 4
3 2
1

Enter the number of rows of floyd’s triangle you want to print: 6
** Inverted floyd triangle **
21 20 19 18 17 16
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1


Floyd’s triangle with stars in java

Let’s learn java program to print floyd’s triangle with stars in java. Here’s the java program,

import java.util.Scanner;
public class FloydTriangleStars 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the number of rows of floyd's triangle you want to print: ");
      int rows = sc.nextInt();
      int number = 1;
      System.out.println("Printing floyd's triangle with stars in java");
      for(int a = 1; a <= rows; a++)
      {
         for(int b = 1; b <= a; b++)
         {
            System.out.print("* ");
            number++;
         }
         System.out.println();
      }
      sc.close();
   }
}


Output:

Enter the number of rows 
of floyd's triangle you want to print: 4
Printing floyd's triangle 
with stars in java
*  
*  *
*  *  *
*  *  *  *

Enter the number of rows 
of floyd's triangle you want to print: 6
Printing floyd's triangle 
with stars in java
*  
*  *
*  *  *
*  *  *  *
*  *  *  *  *
*  *  *  *  *  * 

   

Floyd triangle in java using while loop

Here let’s learn how to print floyd’s triangle in java using while loop. Let’s see java program,

import java.util.Scanner;
public class FloydTriangleWhileLoop 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the number of rows of floyd's triangle you want to print: ");
      int rows = sc.nextInt();
      int number = 1;
      System.out.println("Floyd triangle in java using while loop");
      int a = 1;
      int b = 1;
      while(a <= rows)
      {
         b = 1;
         while(b <= a)
         {
            System.out.println(number + " ");
            number++;
            b++;
         }
         System.out.println();
         a++;
      }
      sc.close();
   }
}


Output:

Enter the number of rows of floyd’s triangle you want to print: 4
Floyd triangle in java using while loop
1
2 3
4 5 6
7 8 9 10

Enter the number of rows of floyd’s triangle you want to print: 6
Floyd triangle in java using while loop
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21


1 01 101 0101 10101 pattern in java

Let’s learn to print the floyds triangle in 1 01 101 0101 10101 pattern in java.

import java.util.Scanner;
public class PatternInJava
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of rows to print pattern: ");
      int number = sc.nextInt();
      int c;
      for(int a = 1; a <= number; a++)
      {
         for(int b = 1; b <= a; b++)
         {
            if((a + b) % 2 == 0)
            {
               c = 1;
               System.out.print(c);
            }
            else
            {
               c = 0;
               System.out.print(c);
            }
         }
         System.out.println();
      }
      sc.close();
   }
}


Output:

Please enter number of rows to print pattern: 5
1
01
101
0101
10101

So this is all about java program to print floyd’s triangle.