ในการค้นหาการเตือนความจำของการคูณอาร์เรย์หารด้วย n โค้ด Java มีดังต่อไปนี้ −
ตัวอย่าง
import java.util.*;
import java.lang.*;
public class Demo{
public static int remainder(int my_arr[], int arr_len, int val){
int mul_val = 1;
for (int i = 0; i < arr_len; i++)
mul_val = (mul_val * (my_arr[i] % val)) % val;
return mul_val % val;
}
public static void main(String argc[]){
int[] my_arr = new int []{ 35, 100, 69, 99, 27, 88, 12, 25 };
int arr_len = 8;
int val = 11;
System.out.println("The remainder when the array is multiplied by 11 is ");
System.out.println(remainder(my_arr, arr_len, val));
}
} ผลลัพธ์
The remainder when the array is multiplied by 11 is 0
คลาสที่ชื่อว่า Demo มีฟังก์ชันสแตติกที่ชื่อว่า 'remainder' ซึ่งจะวนซ้ำผ่านอาร์เรย์จำนวนเต็ม และตรวจสอบเพื่อดูว่าจำนวนเฉพาะแบ่งองค์ประกอบทั้งหมดของอาร์เรย์จำนวนเต็มทั้งหมดหรือไม่ มิฉะนั้น ให้ค่าเตือนความจำ ในฟังก์ชันหลัก ค่าสำหรับตัวเลขถูกกำหนด อาร์เรย์จำนวนเต็มถูกกำหนด และความยาวของอาร์เรย์ถูกกำหนด ฟังก์ชันนี้ถูกเรียกโดยการส่งผ่านอารัตจำนวนเต็ม ความยาวของอาร์เรย์ และตัวเลขเป็นพารามิเตอร์ ข้อความที่เกี่ยวข้องจะแสดงบนคอนโซล