Ask Question
3 February, 08:56

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.

+5
Answers (1)
  1. 3 February, 11:40
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers