To check whether it is possible to make a divisible by 3 number using all digits in an array, the Java code is as follows −
Example
import java.io.*; import java.util.*; public class Demo{ public static boolean division_possible(int my_arr[], int n_val){ int rem = 0; for (int i = 0; i < n_val; i++) rem = (rem + my_arr[i]) % 3; return (rem == 0); } public static void main(String[] args){ int my_arr[] = { 66, 90, 87, 33, 123}; int n_val = 3; if (division_possible(my_arr, n_val)) System.out.println("It is possible to make a number that can be divided by 3"); else System.out.println("It is not possible to make a number that can be divided by 3"); } }
Output
It is possible to make a number that can be divided by 3
A class named Demo contains a function named ‘divide_possible’. It checks to see if the numbers can be used to make a number that can be divided by 3. In the main function, an array with values, and an ‘n’ value is defined. The function is called with the specific arguments and the relevant message will be displayed on the console.