Java Program - Print Floyd's Triangle
Floyd's triangle is named after Robert Floyd and it is a right-angled triangular array of natural numbers. It is created by filling the rows of the triangle with consecutive natural numbers, starting with 1 at the top.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example:
In the example below, a method called FloydTriangle is defined. It requires one parameter to pass as number of rows in the triangle and prints the given number of rows of Floyd's triangle.
public class MyClass { //method for floyd triangle static void FloydTriangle(int n) { int value = 1; for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { System.out.print(value + " "); value++; } System.out.println(); } } public static void main(String[] args) { FloydTriangle(7); } }
The above code will give the following output:
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
Recommended Pages
- Java Program - To Check Prime Number
- Java Program - Bubble Sort
- Java Program - Selection Sort
- Java Program - Maximum Subarray Sum
- Java Program - Reverse digits of a given Integer
- Java - Swap two numbers
- Java Program - Fibonacci Sequence
- Java Program - Insertion Sort
- Java Program - Find Factorial of a Number
- Java Program - Find HCF of Two Numbers
- Java Program - Merge Sort
- Java Program - Shell Sort
- Stack in Java
- Queue in Java
- Java Program - Find LCM of Two Numbers
- Java Program - To Check Whether a Number is Palindrome or Not
- Java Program - To Check Whether a String is Palindrome or Not
- Java Program - Heap Sort
- Java Program - Quick Sort
- Java - Swap Two Numbers without using Temporary Variable