Floyd triangle in Java
In this post, you will learn how to print Floyd triangle pattern in Java.
The Floyd's triangle named after Rober Floyd is a right-angled triangle that contains consecutive natural numbers. It starts from 1 and consecutively selects the next greater number in sequence. Basically, it is arrangement of natural numbers from left to right in a right-angled triangle.
Algorithm
STEP1: Take number of rows to be printed, N.
STEP2: Make outer iteration X for n times to print rows
STEP3: Make inner iteration for Y to X
STEP4: Print Z
STEP5: Increment Z
STEP6: Print NEWLINE character after each inner iteration
STEP7: Return
Pseudocode
We can determine a pseudocode for the above mentioned algorithm, as follows −
// Procedure floyds_triangle
FOR X = 1 to N DO
FOR Y = 1 to X DO
PRINT Z
INCREMENT Z
END FOR
PRINT NEWLINE
END FOR
end procedure
Java program to display floyds triangle
The given java program prints the floyds triangle upto 6th rows.
// Java program to display Floyd's triangle
public class floydTriangle {
public static void main(String[] args)
{
// No of rows to be printed
int n = 6;
// initializing variables for
// rows, columns and display value
int x, y, z = 1;
// Outer loop for rows
for (x = 1; x <= n; x++) {
// Inner loop for columns
for (y = 1; y <= x; y++) {
// Printing value to be displayed
System.out.print(z + " ");
// Incremeting value displayed
z++;
}
// Print elements of next row
System.out.println();
}
}
}
Output of the above code:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number