Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2-when is it more appropriate to use aa for loop over while loop ? Lab 07 Triang

ID: 2268278 • Letter: 2

Question


2-when is it more appropriate to use aa for loop over while loop ? Lab 07 Triangle Maker 9000 rite a program that takes in a positive number and draws a horizontal triangle of asterisks (). The inputted number represents creasing the number of asterisks at each line until i reaches the size of the triangle and then decreases the number of triangles at.cac Example Dialog Welcome to Triangle Maker 9000! Enter the size of the triangle. DONE! Lab Report Questions: 1. Draw a flow chart for the solution 2. When is it more appropriate to use a for-loop over a while-loop? Upload the java file to the dropbo:x

Explanation / Answer

code:

import java.util.Scanner;
class Main {
public static void main(String[] args) {
  
// taking user input
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Triangle Maker 9000! Enter the size of the triangle.");
int n = sc.nextInt();
  
System.out.println();
int i = 1;
  
// printing upper half of the triangle
while(i<=n)
{
// for printing *s in each line
for(int j=1; j<=i; j++)
System.out.print("*");
  
// printing new line
System.out.println();
i++;
}
  
// printing lowe half of the triangle
i=n-1;
while(i>0)
{
// for printing *s in line
for(int j=1; j<=i; j++)
System.out.print("*");
  
// printing new line
System.out.println();
i--;
}
System.out.println();
  
System.out.println("DONE!");
}
}
/*
Welcome to Triangle Maker 9000! Enter the size of the triangle.
3

*
**
***
**
*

DONE
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote