Write a program that asks the user for a positive integer, then creates a triang
ID: 3660766 • Letter: W
Question
Write a program that asks the user for a positive integer, then creates a triangle that has stars in each row from 1 up to the user's number. For example, if the user's number is 5, then the program creates this triangle:
*
**
***
****
*****
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
/* Print a Triangle of Stars on it's base
Input: a positive integer
Output: an n-sized triangle */
Void TriangleTwo (int t) ; {
int i = 0;
int x = 0;
int y = 1;
int counter = 0;
while (counter < t)
{
x = t - (t - y);
for (i = 0; i < x; i++){
printf("*");
}
{
printf(" ");
y++;
counter++;
}
}
}
}
}
i tried it and it keeps telling me
Triangle.java:11: error: ';' expected
Void TriangleTwo (int t) ; {
^
Triangle.java:11: error: ';' expected
Void TriangleTwo (int t) ; {
^
2 errors
Explanation / Answer
/*100% working code*/
import java.util.Scanner;
public class Triangle {
static void triangleTwo(int t) {
int i = 0;
int counter = 1;
while (counter <= t) {
for (i = 1; i <= counter; i++) {
System.out.print("*");
}
System.out.println();
counter++;
}
}
public static void main(String[] args) {
int value;
System.out.println("Enter any number:");
Scanner input = new Scanner(System.in);
value = input.nextInt();
triangleTwo(value);
}
}
Output:-
Enter any number:4
*
**
***
****
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.