How to Find the Product of 2D Array Elements in C++? Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, finding the product of all elements in a 2D array means for a given 2D array of order M*N we have to multiply all the elements together. In this article, we will learn how to find the product of elements in a 2D array in C++. Example Input:myArray[2][2] = {{1, 2}, {3, 4}};Output:Product of Elements: 24Multiplying Elements in a 2D Array in C++To find the product of elements in a 2D array we can simply use loops to iterate through the given 2D array and keep updating the product to product * element at that position. It is important to initialize the product variable to 1 because if we initialize it with 0 then multiplying the elements with 0 results in 0 only. C++ Program for Multiplying Elements in a 2D ArrayThe below example demonstrates how we can find a product of elements in a 2D array. C++ // C++ program to find the product of all elements in a 2D // array #include <iostream> using namespace std; int main() { // creating 2D array int rows = 2; // Number of rows int cols = 2; // Number of columns int arr[rows][cols] = { { 1, 2 }, { 3, 4 } }; // Initialize product to 1 long long product = 1; // Iterate over each element in the 2D array for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { product *= arr[i][j]; } } cout << "The product of all elements in the array is: " << product << endl; return 0; } OutputThe product of all elements in the array is: 24 Time Complexity: O(N * M), N and M are number of rows and columns.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Product of 2D Array Elements in C++? A anjalijhqgt7 Follow Improve Article Tags : C++ Programs C++ STL cpp-array Arrays CPP Examples +2 More Practice Tags : CPPArraysSTL Similar Reads How to Find the Cumulative Product of Elements in an Array in C++? In C++, an array is a data structure that stores elements of the same type in contiguous memory locations. The cumulative product of an array is the result of multiplying all the elements in the array. In this article, we will learn how to find the cumulative product of elements in an array in C++. 2 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 min read How to Find the Mode in a 2D Array in C++? A mode is a number that occurs most frequently in comparison to other numbers in a given dataset. In this article, we will find the mode in a 2D array of integers in C++. Input:myArray = { {1, 2, 2, 3}, {3, 4, 5, 5}, {5, 6, 7, 8} }Output: 5Mode in 2D ArrayTo find a mode of numbers in a 2D array, we 2 min read How to Find the Median of 2D Array in C++? The median can be defined as the middle element of a sorted array in the case of an odd number of elements in an array and the average of the middle two elements when the number of elements in an array is even. In this article, we will see how to calculate the median of a 2D array in C++. Example: I 3 min read How to Find Common Elements in Two Arrays in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the common elements in two arrays in C++. Examples: Input:Arr1: {1, 2, 3, 4, 5}Arr2: {3, 4, 5, 6, 7}Output:Common Elements: 3 4 4 min read How to Find the Range of Values in a 2D Array in C++? In C++, 2D arrays are also known as a matrix, and the range of numbers in a 2D array means the maximum and the minimum value in which the numbers lie in the given 2D array. In this article, we will learn how to find the range of numbers in a 2D array in C++. For Example, Input: my2DArray= {{80, 90, 2 min read How to Find All Indexes of an Element in an Array in C++? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to find all indexes of a specific element in an array in C++. Example: Input: myArray = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4}; target = 3Output: The element 3 occurred at 2 min read How to Find the Variance of Numbers in 2D Array in C++? The variance of numbers is a measure of the spread of a set of values which is used to find how much the values in a dataset differ from mean or average. In this article, we will learn how to find the variance of numbers in a 2D array in C++. Example Input: myMatrix = { { 10, 20, 30 }, { 40, 50, 60 3 min read How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3 2 min read Kth smallest number in array formed by product of any two elements from two arrays Given two sorted arrays A[] and B[] consisting of N and M integers respectively, the task is to find the Kth smallest number in the array formed by the product of all possible pairs from array A[] and B[] respectively. Examples: Input: A[] = {1, 2, 3}, B[] = {-1, 1}, K = 4Output: 1Explanation: The a 15+ min read Like