Write a Java program that asks a user to enter the size of a triangle (an intege
ID: 3628560 • Letter: W
Question
Write a Java program that asks a user to enter the size of a triangle (an integer from 1 to 50). Display the triangle 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. On the next line write one fewer asterisk and continue by decreasing the number of asterisks by one for each successive line until only one asterisk is displayed. Hint: use nested for-loops; the outside loop controls the number of lines to write and the inside loop controls the number of asterisks to display on a line. For example, if the user enters 5, the output would be*
**
***
****
*****
****
***
**
*
Part 2. It is common to print a rotating, increasing list of single-digit numbers at the start of a program's output as a visual guide to number the columns of the output to follow. With this in mind, write nested for-loops to produce the following output, with each line 50 characters wide:
| | | | |
12345678901234567890123456789012345678901234567890
Explanation / Answer
please rate - thanks
didn't understand part 2
so guessed
import java.util.*;
public class drawTriangle{
public static void main(String[] args)
{int i,j,n;
Scanner in= new Scanner(System.in);
System.out.println("Enter size of triangle:");
n=in.nextInt();
for(i=0;i<=n;i++)
{for(j=0;j<i;j++)
System.out.print("*");
System.out.println();
}
for(i=n-1;i>0;i--)
{for(j=0;j<i;j++)
System.out.print("*");
System.out.println();
}
System.out.println();
System.out.println();
for(i=1;i<=50;i++)
{j=i%10;
if(j==0)
System.out.print('|');
else
System.out.print(' ');
}
System.out.println();
for(i=1;i<=50;i++)
System.out.print(i%10);
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.