Floyd Triangle Pattern in JAVA

1. Floyd Triangle is one of the easiest number pattern.

2. Floyd Triangle is a Right-angled Triangle of natural number.

3. Every row of triangle contains the element equal to the row number (For example - row 1 of triangle contain only 1 element , row 2 contain 2 element and so on...).

4. Floyd Triangle is defined by filling the rows of right-angled triangle align to left with the consecutive positive number starts with a 1 in the first row and the successive rows start towards the left with the next number.





Let’s go through the logic to create Floyd Triangle:

  • Step - 1 Ask user for number of rows in triangle.
  • Step - 2 Setting up a Variable equal to 1.
  • Step - 3 Iterate from First row to last.
    • Step - 4 Iterate for the column (number of column in a row must be equal to the row number.)
      • Step - 5 Print the variable which is initialized to 1 in Step 2.
      • Step - 6 Increment the value of variable by 1.
    • Step - 7 Give a line break in order to go to next line for next row.

Code for Pascal Triangle in Java:

package com.missTechy; import java.util.Scanner; public class FloydTriangle { public static void main(String[] args) { // Ask the user for triangle size. System.out.println("Enter the number of rows for Floyd Triangle : "); // Create an instance of Scanner class present in util package. // Object of scanner class is used to take input from user. Scanner scanner = new Scanner(System.in); // Normally when scanner class object is used then it get the value in String format. // scanner object calls nextInt() method which is used to convert String into Int. int rows = scanner.nextInt(); // This variable is used to Print the value of triangle starts from 1. int value = 1; // Iterate from first row to last. for(int currentRow = 0 ; currentRow < rows ; currentRow++ ) { // Iterate for column. // Column <= currentRow i.e. a row must contain column equalTo or less than currentRow number. for(int column = 0 ; column<=currentRow ; column++) { // Print the variable for triangle value. System.out.print(value + " "); // Floyd triangle contain the natural number. // for the next value increment the variable by 1. value++; } // This s.o.p takes to the next line. System.out.println(); } } }

Output of Pascal Pattern in Java:

Post a Comment

0 Comments