Basic Right Triangle Number Pattern
Program to Print Basic Right Triangle Number Pattern
PRINTING PATTERN:
1 23 456 78910
PREREQUISITE:
Basic knowledge of Java language and loops.
ALGORITHM:
1. Take the number of rows (n) as input from the user. This will determine the height of the triangle.
2. Initialize a variable (let's call it 'num') to 1. This variable will be used to print the numbers in the pattern.
3. Initialize a loop that runs from 1 to n (inclusive) to iterate through each row.
4. For each row (let's call the current row index 'i'), initialize another loop that runs from 1 to 'i' (inclusive) to print the numbers in that row.
5. Inside the inner loop, print the current value of 'num', and then increment 'num' by 1.
6. After finishing the inner loop for a row, print a newline character to move to the next row.
7. Repeat steps 3 to 6 until all rows are printed.
Code in Java:
import java.util.Scanner; public class BasicRightTriangleNumberPattern { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the number of rows for the right triangle number pattern:"); int n = scanner.nextInt(); // Taking input for number of rows int num = 1; // Variable to keep track of the current number // Loop to print the right triangle number pattern for (int i = 1; i <= n; i++) { // Loop for each row for (int j = 1; j <= i; j++) { // Loop for each column in the current row System.out.print(num); // Printing the current number num++; // Incrementing the number } System.out.println(); // New line after each row } scanner.close(); } }
Output:
Enter the number of rows for the right triangle number pattern: 4 1 23 456 78910