65 776
Assignments Done
99,4%
Successfully Done
In October 2018

Answer to Question #64581 in Java | JSP | JSF for Sherlynn

Question #64581
Write a JAVA program that asks the user to enter a number. Display the following pattern by writing lines of asterisks.
The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line,up to the number entered by the user.For example, if the user enters 5, the output would be:
*
* *
* * *
* * * *
* * * * *
Expert's answer
/**
*
* Write a JAVA program that asks the user to enter a number.
* Display the following pattern by writing lines of *asterisks.
* The first line will have one asterisk, the next two, and so on,
* with each line having one more asterisk than the *previous line,
* up to the number entered by the user.
* For example, if the user enters 5, the output would be:
* *
* * *
* * * *
* * * * *
* * * * * *
*/
import java.util.Scanner;
public class Question64581 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a positive integer number:");
if (sc.hasNextInt())
printAsterisk(sc.nextInt());
else System.out.println("Incorrect number or not a number");
}

public static void printAsterisk(int n) {
StringBuilder sb = new StringBuilder("*");
for (int i = 0; i < n; i++) {
System.out.println(sb);
sb.append(" *");
}
}
}

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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions