Printing triangles: Write a program which inputs a positive integer n and outputs an n line high triangle of '*' characters whose right-angle is in the bottom left corner. For example, if 5 is input the output should be
Sample Run1
Enter a positive integer: 5
Output1:
*
**
***
****
*****
import java.util.Scanner;
public class App {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = keyBoard.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
keyBoard.close();
}
}
Comments
Leave a comment