Write a Java Program to Print Floyd’s Triangle using For Loop
To print the Floyd’s triangle in Java programming, you have to use two for loops, the outer loop is responsible for rows and the inner loop is responsible for columns and start printing the Floyd’s triangle as shown in the following program.
As we know that, Floyd’s triangle is a right angled-triangle using the natural numbers, so the following Java Program ask to the user to enter the range ( how many line he/she want) to print the Floyd’s Triangle.
- Following is the sample of Floyd’s Triangle :
SOURCE CODE ::
import java.util.Scanner; public class FloydTriangle { public static void main(String[] args) { int i,j,k=1,n; System.out.print("Enter how many lines u want : "); Scanner sc = new Scanner(System.in); n=sc.nextInt(); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { System.out.print(" "+k); k++; } System.out.println(""); } } }
OUTPUT ::
Enter how many lines u want : 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21