Question

In: Computer Science

Write a Java program that asks the user to enter an integer that is used to...

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

Solutions

Expert Solution

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.


Related Solutions

Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
Design, plan, test, and write a computer program in Java that asks the user to enter...
Design, plan, test, and write a computer program in Java that asks the user to enter 1 number and a String. You will display the first n characters of the string where n is the number entered. For example, if the user enters 3 and java then you will print jav. If they enter 5 and Halloween then you print Hallo. If the user enters a number less than 0 then set the number to 0. Assume the user will...
Write a java program which asks the user to enter name and age and calls the...
Write a java program which asks the user to enter name and age and calls the following methods: printName(): Takes name as parameter and prints it 20 times using a while loop. printAge(): Takes age as parameter and prints all the numbers from 1 up to age. Write a java program that will print first 10 multiples of 3 in a single line.
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT