Question: Please read carefully: I need to do the recursion method to see if a set of arrays are divisible by 3 and 4 using only
Please read carefully:
I need to do the recursion method to see if a set of arrays are divisible by 3 and 4 using only a boolean method, using JAVA.
The integers need to be divisible by both 3 and 4 to be TRUE.
But if one integer in the array is only divisible by 3 and not 4 then it is FALSE, or if it is only divisible by 4 and not 3 it is FALSE.
For example:
{15, 9, 8} = TRUE; 15 and 9 are divisible by 3, and 8 is divisible by 4.
{15, 55, 7} = FALSE; 15 is only divisble by 3, 55 and 7 are not divisible by either or.
{empty array} = always false
My boolean method has a lot of issues so any revisement would be nice but please use recursion and DO NOT add any other methods.
Now here is my code so far:
import java.util.Scanner;
public class Practice { public static boolean divisible(int[] array) { int i = 0; if (array.length == 0) { // for empty arrays return false; } if (array[i] % 3 == 0 && array[i] % 4 == 0) { ++i; //wanting it to increase the i and check the next number to see if it is divisible return true && divisible(array); } else return false; } public static void main(String[] args) { Scanner scnr = new Scanner(System.in);
System.out.print("How many numbers will you put in? "); int j = scnr.nextInt(); int[] array = new int[j];
for (int i = 0; i < j; ++i) { System.out.print("Enter array(" + i + ") "); array[i] = scnr.nextInt(); System.out.println("array(" + i + ") = " + array[i]); } if (divisible(array) == false) System.out.println("Your array is false"); else System.out.println("Your array is true"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
