Write a C++ Program to Multiply Two Arrays with an example. In this C++ multiplication of two arrays example, we allow the user to enter the multiarr1, multiarr2 array sizes and array items. Next, we used the C++ for loop to iterate the multiarr1 and multiarr2 arrays from 0 to size.
Within the for loop, we performed multiplication on both the array items and assigned them to a new multiplication array. Here, we also used the cout statement (cout << multiarr1[i] << ” * ” << multiarr2[i] << ” = ” << multiplication[i] << “\n”;) to show you the result at each for loop iteration. Finally, we used one more for loop to print the multiplication array items.
#include<iostream> using namespace std; int main() { int size, i, multiarr1[10], multiarr2[10], multiplication[10]; cout << "\nPlease Enter the Array Size = "; cin >> size; cout << "\nPlease Enter the First Array Items = "; for(i = 0; i < size; i++) { cin >> multiarr1[i]; } cout << "\nPlease Enter the Second Array Items = "; for(i = 0; i < size; i++) { cin >> multiarr2[i]; } for(i = 0; i < size; i++) { multiplication[i] = multiarr1[i] * multiarr2[i]; cout << multiarr1[i] << " * " << multiarr2[i] << " = " << multiplication[i] << "\n"; } cout << "\nThe Final Result of Multiplying two Arrays = "; for(i = 0; i < size; i++) { cout << multiplication[i] << " "; } return 0; }
C++ Program to Multiply Two Arrays Example 2
In this C++ multiplication of two arrays example, we removed that extra for loop to display the items. Next, we placed a cout statement after performing the multiplication.
#include<iostream> using namespace std; int main() { int size, i, multiarr1[10], multiarr2[10], multiplication[10]; cout << "\nPlease Enter the Array Size = "; cin >> size; cout << "\nPlease Enter the First Array Items = "; for(i = 0; i < size; i++) { cin >> multiarr1[i]; } cout << "\nPlease Enter the Second Array Items = "; for(i = 0; i < size; i++) { cin >> multiarr2[i]; } cout << "\nThe Final Result of Multiplying two Arrays = "; for(i = 0; i < size; i++) { multiplication[i] = multiarr1[i] * multiarr2[i]; cout << multiplication[i] << " "; } return 0; }
Please Enter the Array Size = 6
Please Enter the First Array Items = 10 20 30 40 50 60
Please Enter the Second Array Items = 4 5 6 7 8 9
The Final Result of Multiplying two Arrays = 40 100 180 280 400 540