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 triangle of array of natural numbers starting with 1 in the top left corner.

In floyd’s 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 to print 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 row, 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: ");
      row = sc.nextInt();
      System.out.println("Floyd's Triangle: ");
      for(y = 1; y <= row; y++)
      {
         for(z = 1; z <= y; z++)
         {
            System.out.print(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:
*  $ $ $ $ 
*  $ $ $ 
*  $ $ 
*  $ 
*/
import java.util.Scanner;
public class FloydTriangleDemo 
{
   public static void main(String[] args) 
   {
      int a, b;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter any character: ");
      char ch = ((sc.nextLine()).charAt(0));
      System.out.print("Please enter number of rows of floyd's triangle you want to print: ");
      int row = sc.nextInt();
      System.out.println("Inverted Floyd's Triangle: ");
      for(a = row; a >= 1; a--)
      {
         for(b = 1; b <= a; b++)
         {
            System.out.print(ch);
         }
         System.out.println();
      }
      sc.close();
   }
}


Output:

Please enter any character:
$
Please enter number of rows of floyd’s triangle you want to print: 4
Inverted Floyd’s Triangle:
$$$$
$$$
$$
$

Please enter any character:
&
Please enter number of rows of floyd’s triangle you want to print: 5
Inverted Floyd’s Triangle:
&&&&&
&&&&
&&&
&&
&


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();
      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("* ");
         }
         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)
   {
      int a, b, x, y;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of rows to print pattern: ");
      int rows = sc.nextInt();
      for(a = 1; a <= rows; a++)
      {
         if(a % 2 == 0)
         {
            x = 1;
            y = 0;
         }
         else
         {
            x = 0;
            y = 1;
         }
         for(b = 1; b <= a; b++)
         {
            if(b % 2 == 0)
            {
               System.out.print(x);
            }
            else
            {
               System.out.print(y);
            }
         }
         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.