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...
In a file called DivisibilityTests.java, write a program that: Asks the user to enter an integer....
In a file called DivisibilityTests.java, write a program that: Asks the user to enter an integer. It is OK for your program to crash when the user does not enter a valid integer. If the integer is less than 0, the program prints: "The number is negative." If the integer is divisible by 2 and by 3, the program prints: "The number is even and divisible by 3." If the integer is divisible by 2 but not by 3, the...
In java, write a program that asks the user to enter a character. Then pass the...
In java, write a program that asks the user to enter a character. Then pass the character to the following methods. isVowel() – returns true if the character is a vowel isConsonant() – returns true if the character is a consonant changeCase() – if the character is lower case then change it to upper case and if the character is in upper case then change it to lower case. (return type: char) Example output is given below: Enter a character...
Write a java program that asks the user for a positive integer N and then calculates...
Write a java program that asks the user for a positive integer N and then calculates a new value for N based on whether it is even or odd: if N is even, the new N is N/2. (Use integer division.) if N is odd, the new N is 3*N + 1. Repeat with each new N until you reach the value 1. For example, say the initial value is 12. Then the successive values are: 12 (even, next value...
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 C program that does the following: 1) Asks the user to enter an integer...
Write a C program that does the following: 1) Asks the user to enter an integer N 2) Asks the user to enter three words with at least N letters and at most 20 letters 3) Prints the first N letters from the first word 4) Prints the last N letters from the second word 5) Prints all letters in the odd position in the third word Output: Please enter an integer N: 3 Please enter a word with at...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT