The following program demonstrates how to Display Floyd’s Triangle in Java.

In this program, the number of rows in the triangle is provided by the user as a command line argument. If the user doesn’t specify the number in the command line, it displays an error message and exits. However, if the user specifies more than one argument, it displays another error message and exits. When the user enters exactly one number, it displays the triangle of ‘*’ using the nested for loop.

import java.util.Scanner;
import java.lang.Math;
public class Exercise15
{
	public static void main(String[] args) {
		if(args.length==0)
		{
			System.out.println("Please enter an integer number");
			System.exit(0);
		}
		if(args.length>1)
		{
			System.out.println("Wrong Number of Command Line Arguments!\nExiting...");
			System.exit(0);
		}
		int num_rows=Integer.parseInt(args[0]);
		for(int i=1;i<=num_rows;i++)
		{
			for(int j=1;j<=i;j++)
			{
				System.out.print("* ");	
			}
			System.out.println();
		}	
	}
}

Output

A Program to Display Floyd's Triangle in Java
A Program to Display Floyd’s Triangle in Java

Further Reading

Java Practice Exercise

programmingempire

Princites