Instructions
Write a Java code
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
What is the size of the array? 3
Give me 3 numbers. 10 20 15
10: !MULT15: !MULT20
20: !MULT15: MULT20
15: MULT15: !MULT20
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!
Get Answers For Free
Most questions answered within 1 hours.