Home »
Python »
Python programs
Python program to find remainder of array multiplication divided by divisor
Here, we will see a Python program to find the remainder of the product of array when divided by a divisor.
By Shivang Yadav Last updated : March 05, 2024
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Finding remainder of array multiplication divided by divisor
In this program, we will take an array and divisor as input from the user and then print the value of array multiplication divided by divisor.
Example
Input:
4
3 7 1 9
4
Output:
1
This problem can be solved easily by looping over the array and finding the product of all elements. Then finding the remainder of this product by N.
Algorithm
- Initialize prod = 1.
- Loop through the array for i -> 0 to n
- Print the value of (prod % D).
Program to find the remainder of array multiplication divided by divisor
# Python program to find the remainder
# of array multiplication by divisor
# Function to find the remainder of
# array multiplication in Python
def findArrMulRemainder(arr, n, R):
prodVal = 1
for i in range(n):
prodVal *= arr[i]
return (prodVal % R)
# Taking array input from user
n = int(input("Enter number of elements of the array: "))
arr = []
print("Enter elements of the array: ")
for i in range(n):
numbers = int(input())
arr.append(numbers)
D = int(input("Enter divisor: "))
rem = findArrMulRemainder(arr, n, D)
print("The remainder of array multiplication by divisor is ", rem)
Output
Enter number of elements of the array: 4
Enter elements of the array:
5
7
2
1
Enter divisor: 4
The remainder of array multiplication by divisor is 2
Explanation
In the above code, we have created a variable prodVal. And store the product of all elements of the array. Then we have returned the value of prodVal % d and printed it.
This solution is easy to implement but can lead to overflow as the product values can exceed the data's maximum value.
Commutative Approach
So, an effective approach would be using the commutative approach to solve the problem. This will save memory loss, and computation overhead.
This is based on: (a * b)%c = ((a%c) * (b%c))%c
We will find arr[i]%D for each element of the array and find their products. Then, find the remainder of this product with D.
Algorithm
- Initialize prod = 1.
- Loop through the array for i -> 0 to n
- Print the value of (prod % D).
Program to find the remainder of array multiplication divided by divisor
# Python program to find the remainder
# of array multiplication by divisor
# Function to find the remainder of
# array multiplication in Python
def findArrMulRemainder(arr, n, R):
prodVal = 1
for i in range(n):
prodVal *= (arr[i]%R)
return (prodVal % R)
# Taking array input from user
n = int(input("Enter number of elements of the array: "))
arr = []
print("Enter elements of the array: ")
for i in range(n):
numbers = int(input())
arr.append(numbers)
D = int(input("Enter divisor: "))
rem = findArrMulRemainder(arr, n, D)
print("The remainder of array multiplication by divisor is ", rem)
Output
Enter number of elements of the array: 4
Enter elements of the array:
5
7
2
1
Enter divisor: 4
The remainder of array multiplication by divisor is 2
Explanation
In the above code, we have created a variable prodVal. And, store the product of (arr[i] % d) for all elements of the array. Then we have returned the value of prodVal % d and printed it.
To understand the above programs, you should have the basic knowledge of the following Python topics:
Python Array Programs »