Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Frequently Asked Java Program 06: Print a Floyd Triangle of given number of rows

Posted on 01/13/2025 By admin

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a Java program to Print a Floyd Triangle of given number of rows.

Problem: Write a java program to print a Floyd’s triangle for a given number of rows.
Solution:

Floyd’s triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Logic to draw such pattern:

  • In JAVA, such patterns are known as pyramid. We can easily draw using matrix.
  • First, we think about row count which will be user input. If user enters 5, there will be 5 rows in triangle. For N, there will be N rows in triangle. So we need to have a for loop which will keep track of number of rows. It must start with 1 and iterate till N.
  • Now consider number of columns. For first row, there is only column. For second row, there are only two columns. For nth row, there should be only N columns. So we need to use another for loop, which will start from 1 and keep counter till it reaches number of row.
  • Since, we need to print natural numbers in a order, we will initialize a variable and increment it after printing once.

Java Program:

import java.util.Scanner;

public class FloydTriangle {

        public static void main(String[] args) {
                
                // input for number of rows
                Scanner in = new Scanner(System.in);
                System.out.println("Enter the number of rows: ");
                int r = in.nextInt();
                int k=1;
                System.out.println("Floyd Triangle of rows: "+r);
                // for number of rows
                for(int i=1;i
Uncategorized

Post navigation

Previous Post: ThreadLocal Static WebDriver For Parallel Execution
Next Post: TestNG Tutorials 68 : Sharing Data Between Tests in a Suite Using ISuite & ITestContext

Related Posts

Postman Tutorial Part 53 – Extracting Value From XML Response in Postman Uncategorized
image – Make Selenium Easy Uncategorized
Postman Tutorial Part 42 – Create Parameterized Request by Reading Data From CSV in Postman – Make Selenium Easy Uncategorized
allAreWebElements – Make Selenium Easy Uncategorized
Selenium Topics – Page 13 – Make Selenium Easy Uncategorized
Selenium Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark