Question

Instructions Write a Java code Your goal is to take N integer inputs from the user...

Instructions

Write a Java code

  • Your goal is to take N integer inputs from the user -- N's value will be given by the user as well.
    • You can assume the user provides a valid value for N, i.e., >0.
  • Store the input integers in an array of size N in the order they are provided.
    • These tasks should be done in the main() method.
  • Create a new method called checkArray() that will take the previously created array as input from main().
  • Decide whether each member of the array is a multiple of 15 and 20 or not.
  • And, print the decision in the same way as shown below.
  • You can use the code structure given below.
  • If you do not follow any of the instructions, you will lose points.

Code Structure

main(){

N = take input from user;

arr = create a N-size array.

populate the array with user inputs.

call the checkArray() method and send arr.

}

checkArray(){

for each array member of arr, check if it is a multiple of 15 and 20 or not.

print the results.

}

Sample Output

  • Your output should look exactly like the following; otherwise, you will lose points.

What is the size of the array? 3

Give me 3 numbers. 10 20 15

10: !MULT15: !MULT20

20: !MULT15: MULT20

15: MULT15: !MULT20

Homework Answers

Answer #1

Have a look at the below code. I have put comments wherever required for better understanding.

import java.util.*;

class Main {
  // create the checkArray function
  public static void checkArray(int[] arr, int n){
    // loop in through the array
    for(int i=0;i<n;i++){
      // check if number is divisible by both 15 and 20
      if ((arr[i]%15==0)&&(arr[i]%20==0)){
        System.out.println(arr[i]+":"+"MULT15"+":" + "MULT20");
      }
      // check if number is divisible by 15 but not 20
      if ((arr[i]%15==0)&&(arr[i]%20!=0)){
        System.out.println(arr[i]+":"+"MULT15"+":" + "!MULT20");
      }
      // check if number is divisible by 20 but not 15
       if ((arr[i]%15!=0)&&(arr[i]%20==0)){
        System.out.println(arr[i]+":"+"!MULT15"+":" + "MULT20");
      }
      // check if number is not divisible by both 15 and 20
       if ((arr[i]%15!=0)&&(arr[i]%20!=0)){
        System.out.println(arr[i]+":"+"!MULT15"+":" + "!MULT20");
      }
    }



  }
  public static void main(String[] args) {
    // create scanner object for user input
    Scanner sc = new Scanner(System.in);
    // create variable for n
    int n;
    // take user input for n
    n = sc.nextInt();
    // create an array of size n
    int arr[] = new int[n];
    // populate the array
    for (int i=0;i<n;i++){
      arr[i] = sc.nextInt();
    }
    // call the checkArray function
    checkArray(arr,n);
  }
}

Happy Learning!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please write a java program Instructions Your goal is to take N integer inputs from the...
Please write a java program Instructions Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two different classes in the program being made as objects in the main (Bunny class and Dog Class). ask the user "what kind of animal would you like to sort?" user chooses the type of animal (bunny or dog), then you ask the user how big would you like the array of animals to be (maximum array size = 20)? and then you ask what...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
JAVA Write code which takes two inputs from the user, a number of sides followed by...
JAVA Write code which takes two inputs from the user, a number of sides followed by a side length, then creates a regular polygon with that number of sides and side length. Sample run: Type the number of sides: 8 Type a side length: 7.5 regular octagon with side length 7.5 Hint: Make sure you use the right data types when taking user input.
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
Write a program that takes n integer numbers from the user, and then counts the number...
Write a program that takes n integer numbers from the user, and then counts the number of even numbers and odd numbers and print them to the screen. Sample Output: Enter how many numbers you have: 10 Enter the 10 numbers: 1 3 19 50 4 10 75 20 68 100 The number of even numbers is: 6 The number of odd numbers is: 4 (( in java ))
Write a program which inputs an integer value, incr, from the user at the keyboard, and...
Write a program which inputs an integer value, incr, from the user at the keyboard, and then displays a graph of the sine of the degree values from 0 to 180, in increments of incr. You must use the sin function (in a similar manner to ceil and floor) to find the sine. Note that the sin function only operates on radians so the degree value must first be converted to radians. To do so, multiply the degree value by...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT