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

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...
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...
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...
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...
Instructions Write a Java program that asks the user t enter five test scores. The program...
Instructions Write a Java program that asks the user t enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: * calcAverage -- This method should accept five test scores as arguments and return the average of the scores. * determineGrade -- This method should accept a test score as an argument and return a letter grade for the score, based on the following...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Write a java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT