Java Program to find reminder of array multiplication divided by n

JavaServer Side ProgrammingProgramming

To find reminder of array multiplication divided by n, the Java code is as follows −

Example

 Live Demo

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));
   }
}

Output

The remainder when the array is multiplied by 11 is
0

A class named Demo contains a static function named ‘remainder’, that iterates through an integer array, and checks to see if a specific number divides all the elements of the integer array completely, otherwise give the reminder values. In the main function, the value for the number is defined, an integer array is defined, and the length of the array is defined. The function is called by passing the integer arrat, length of array, and number as parameters. Relevant message is displayed on the console.

raja
Published on 08-Jul-2020 11:57:48
Advertisements