In: Computer Science
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops
•Ascending multiples of five with ascending length triangle
•Ascending multiples of five with descending length (inverted) triangle
•Descending multiples of five with ascending length triangle
•Descending multiples of five with descending length (inverted) triangle
Use error checking to keep asking the user for a positive number until they enter one.
Expected Output/Sample Runs (Do NOT just hardcode output)
Enter max number of multiples: 6
Pattern A:
5
5 10
5 10 15
5 10 15 20
5 10 15 20 25
5 10 15 20 25 30
Pattern B:
5 10 15 20 25 30
5 10 15 20 25
5 10 15 20
5 10 15
5 10
5
Pattern C:
5
10 5
15 10 5
20 15 10 5
25 20 15 10 5
30 25 20 15 10 5
Pattern D:
30 25 20 15 10 5
25 20 15 10 5
20 15 10 5
15 10 5
10 5
5
Enter max number of multiples: -2
Please enter a positive value.
Enter max number of multiples: -3
Please enter a positive value.
Enter max number of multiples: 4
Pattern A:
5
5 10
5 10 15
5 10 15 20
Pattern B:
5 10 15 20
5 10 15
5 10
5
Pattern C:
5
10 5
15 10 5
20 15 10 5
Pattern D:
20 15 10 5
15 10 5
10 5
5
The task is to implement a JAVA program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops. If the user gives an invalid input, the program would ask the user for a positive number until they enter one.
There are four patterns for each input and each pattern will
output multiples of 5. Using two nested for loops,
the patterns can be implemented. Each patterns depend on the two
loop variables. The loop variables can be incremented or
decremented to get the required patterns. In case the user gives a
non-positive input, the program will ask the user to input a
positive number till the user input a valid number. This
can be implemented using do-while loop.
The required JAVA code is given below:
import
java.util.*;
//import basic package
public class Main
//declaring a class
{
public static void main(String
[]args) //main method
{
int i,j,n;
Scanner sc=new
Scanner(System.in);
do
{
System.out.printf("Enter max number of multiples: ");
n=sc.nextInt();
//user input of the number of multiples
System.out.printf("Please enter a positive
value.\n"); //asking for a valid input
}while(n<1);
//checking if the input is invalid
System.out.printf("Pattern A:\n");
//printing the pattern A
for(i=1;i<=n;i++)
//creating the outer loop
{
for(j=1;j<=i;j++) //creating
the inside loop
System.out.printf("%d ",5*j);
//multiplying the variable by 5
System.out.printf("\n");
//printing a new line before the next line
}
System.out.printf("Pattern B:\n");
//printing the pattern B
for(i=n;i>=1;i--)
//creating the outer loop
{
for(j=1;j<=i;j++) //creating
the inside loop
System.out.printf("%d ",5*j);
//multiplying the variable by 5
System.out.printf("\n");
//printing a new line before the next line
}
System.out.printf("Pattern C:\n");
//printing the pattern C
for(i=1;i<=n;i++)
//creating the outer loop
{
for(j=i;j>=1;j--) //creating
the inside loop
System.out.printf("%d ",5*j);
//multiplying the variable by 5
System.out.printf("\n");
//printing a new line before the next line
}
System.out.printf("Pattern D:\n");
//printing the pattern D
for(i=n;i>=1;i--)
//creating the outer loop
{
for(j=i;j>=1;j--) //creating
the inside loop
System.out.printf("%d ",5*j);
//multiplying the variable by 5
System.out.printf("\n");
//printing a new line before the next line
}
}
}
Screenshot of the code is given below:
Output (for valid input):
Output (for invalid input):
NOTE: Notice that inside every loop, the variable of the nested loop is printed (since the patterns depend on the row-number). Also depending on the increment or decrement of the loop variables, ascending or descending pattern is printed.