Answer to Question #250360 in Java | JSP | JSF for guru

Question #250360
  1. 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 integer5

Output1:

*

**

***

****

*****

 

Sample Run2

Enter a positive integer-36

Output1: The number entered is not positive

 

Sample Run3

Enter a positive integer10

Output1:

*

**

***

****

*****

******

*******

********

*********

**********


1
Expert's answer
2021-10-13T10:43:44-0400
import java.util.Scanner;


public class App {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		System.out.print("Enter a positive integer: ");
		int n = keyBoard.nextInt();


		if (n < 0) {
			System.out.println("The number entered is not positive");
		} else {
			for (int i = 1; i <= n; i++) {
				for (int j = 0; j <i; j++) {
					System.out.print("*");
				}
				System.out.println();
			}


		}


		keyBoard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS