Java Online Compiler
Example: Floyd's Triangle Pattern Program in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Floyd's Triangle Pattern Program import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Initialize Scanner for user input Scanner scanner = new Scanner(System.in); // Step 2: Prompt user for the number of rows System.out.print("Enter the number of rows for Floyd's Triangle: "); int numRows = scanner.nextInt(); // Step 3: Initialize a counter for the numbers to be printed int currentNumber = 1; // Step 4: Outer loop for controlling the number of rows for (int i = 1; i <= numRows; i++) { // Step 5: Inner loop for controlling elements in the current row // Each row 'i' has 'i' numbers for (int j = 1; j <= i; j++) { // Step 6: Print the current number followed by a space System.out.print(currentNumber + " "); // Step 7: Increment the number for the next print currentNumber++; } // Step 8: Move to the next line after completing a row System.out.println(); } // Step 9: Close the scanner to prevent resource leaks scanner.close(); } }
Output
Clear
ADVERTISEMENTS