How to print Floyd’s Triangle in Java? Example Tutorial
A good programmer should be able to look at a pattern and break it into nested loops. A more formal definition of Floyd’s triangle: “It’s a right-angled triangle of an array of natural numbers, which is named after Robert Floyd.
It looks like the following pattern :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Your task is to print this table using nested loops in Java. The main point is to build the logic by yourself, this will help you a lot in the long run.
Also, basic knowledge of essential data structure and algorithms is also very important and that’s why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course like Data Structures and Algorithms: Deep Dive Using Java on Udemy to improve their knowledge and algorithms skills.
Java Program to draw Floyd’s Triangle
Here is my sample code example to draw a format representing Floyd’s triangle.
This program first asks the user to enter a number of rows till you want to show Floyd’s triangle.
import java.util.Scanner;
/** * Java program to print Floyd's triangle up-to a given row * * @author Javin Paul */ public class FloydTriangle { public static void main(String args[]) { Scanner cmd = new Scanner(System.in); System.out.println("Enter the number of rows of Floyd's triangle, you want to display"); int rows = cmd.nextInt(); printFloydTriangle(rows); } /** * Prints Floyd's triangle of a given row * * @param rows */ public static void printFloydTriangle(int rows) { int number = 1; System.out.printf("Floyd's triangle of %d rows is : %n", rows); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(number + " "); number++; } System.out.println(); } } } Output Enter the number of rows of Floyd's triangle, you want to display 5 Floyd's triangle of 5 rows is : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Enter the number of rows of Floyd's triangle, you want to display 10 Floyd's triangle of 10 rows is : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
That’s all about how do you print Floyd’s triangle in Java. It’s a very good programming exercise to build your foundation on loops especially nested loops. After that, you can also try a couple of other pattern-based programming tasks’ e.g. printing Pascal’s triangle and some other patterns.
- How to count occurrences of a character in String? (Solution)
- How to find first non repeated characters from String in Java? (See here for solution)
- Write a Program to check if a number is binary in Java? (Solution)
- How to remove duplicates from array without using Collection API? (Solution)
- Write a Program to calculate Sum of Digits of a number in Java? (Solution)
- How to Swap Two Numbers without using Temp Variable in Java? (Trick)
- How to check if LinkedList contains loop in Java? (Solution)
- Write a Program to Check if a number is Power of Two or not? (Answer)
- How to find the middle element of LinkedList in one pass? (See here for Solution)
- How to check if a number is Prime or not? (Solution)
- Write a Program to calculate factorial using recursion in Java? (Click here for solution)
- How to check if a number is Palindrome or not? (Solution)
- How to check if Array contains duplicate number or not? (Solution)
- How to remove duplicates from ArrayList in Java? (Solution)
- Write a Java Program to See if two String are Anagram of each other? (Solution)
- Write a Program to find Fibonacci Series of a Given Number? (Solution)
- How to check if a number is an Armstrong number or not? (Solution)
- Write a Program to prevent Deadlock in Java? (Click here for solution)
- Write a Program to solve Producer-Consumer Problem in Java. (Solution)
- How to reverse String in Java without using API methods? (Solution)
- How to print prime factors of a number in Java? (Solution)
And lastly one question for you? Which one is your favorite Java coding exercise? Palindrome, Prime number, Fibonacci, Factorial or this one? Do let me know in comments.
Feel free to ask any question you may have?