Ask Question
23 November, 20:33

Write a program that asks the user to enter the size of a triangle (an integer from 1 to 50) and validate this input. Make sure that your program loops until it receives valid input. Display the triangle by displaying lines of asterisks.

+4
Answers (1)
  1. 23 November, 21:14
    0
    import java. util. Scanner;

    public class num1 {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    int size;

    do{

    System. out. println ("Enter the size of the traingle");

    size = in. nextInt ();

    }while (size > 50);

    //Using nexted for loop to print the triangle

    for (int i=0; i
    {

    for (int j=0; j<=i; j++)

    {

    System. out. print (" * ");

    }

    System. out. println ();

    }

    }

    }

    Explanation:

    Use Scanner class to receive user input

    Validate using a do while statement while (num>50) keep prompting the user for a valid input

    Use nexted for loops to print the triangle (right-angled triangle call a half pyramid with stars)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that asks the user to enter the size of a triangle (an integer from 1 to 50) and validate this input. Make sure that your ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers