Triangle in Java
Q. Write a Java program to print a number triangle
import java.util.Scanner;
public class NumberTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user to enter number of rows
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
// Print number triangle pattern
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println(); // Move to next line after each row
}
scanner.close();
}
}
Q. Write an algorithm to print a number triangle
1. Start
2. Read the number of rows as input and store in rows
3. Loop i from 1 to rows
a. For each i, loop j from 1 to i
b. Print j followed by a space
c. After inner loop ends, print a new line
4. Repeat until all rows are printed
5. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com