C++ Program to Perform Arithmetic Operations on Array

Write a C++ Program to Perform Arithmetic Operations on Arrays such as addition, subtraction, division, multiplication, and modules with an example. In this C++ array arithmetic operations example, we allow the user to enter the arr1 and arr2 array sizes and array items. Next, we used the C++ for loop to iterate the arr1 and arr2 array from 0 to size. Within the for loop, we performed arithmetic operations on both the array items and assigned them to new arrays. Finally, we used one more for loop to print the add, sub, mul, mod, and div array items.

#include<iostream>
using namespace std;

int main()
{
	int size, i, arr1[10], arr2[10];
	int add[10], sub[10], mul[10], mod[10];
	float div[10];
	
	cout << "\nPlease Enter the Array Size =  ";
	cin >> size;
	
	cout << "\nPlease Enter the First Array Items =  ";
	for(i = 0; i < size; i++)
	{
		cin >> arr1[i];
	}	
	cout << "\nPlease Enter the Second Array Items =  ";
	for(i = 0; i < size; i++)
	{
		cin >> arr2[i];
	}
	for(i = 0; i < size; i++)
	{
		add[i] = arr1[i] + arr2[i]; 
		sub[i] = arr1[i] - arr2[i];
		mul[i] = arr1[i] * arr2[i];
		mod[i] = arr1[i] / arr2[i];
		div[i] = arr1[i] % arr2[i];
	}
	cout << "\nAdd\t Sub\t Mul\t Div\tMod = \n";
	for(i = 0; i < size; i++)
	{
		cout << add[i] << "\t";
		cout << sub[i] << "\t";
		cout << mul[i] << "\t";
		cout << mod[i] << "\t";
		cout << div[i] << "\t\n";
	}

 	return 0;
}
C++ Program to Perform Arithmetic Operations on Array 1

C++ Program to Perform Arithmetic Operations on Array Example 2

This C++ program is the same as above. Here, we used the cout statement to show the result of for loop in iteration-wise. 

#include<iostream>
using namespace std;

int main()
{
	int size, i, arr1[10], arr2[10];
	int add[10], sub[10], mul[10], mod[10];
	float div[10];
	
	cout << "\nPlease Enter the Array Size =  ";
	cin >> size;
	
	cout << "\nPlease Enter the First Array Items =  ";
	for(i = 0; i < size; i++)
	{
		cin >> arr1[i];
	}	
	cout << "\nPlease Enter the Second Array Items =  ";
	for(i = 0; i < size; i++)
	{
		cin >> arr2[i];
	}
	for(i = 0; i < size; i++)
	{
		add[i] = arr1[i] + arr2[i]; 
		sub[i] = arr1[i] - arr2[i];
		mul[i] = arr1[i] * arr2[i];
		mod[i] = arr1[i] / arr2[i];
		div[i] = arr1[i] % arr2[i];
		cout << "\nThe Result of the " << i + 1 << " Iteration\n";
		cout << arr1[i] << " + " << arr2[i] << " = " << add[i] << "\n";
		cout << arr1[i] << " - " << arr2[i] << " = " << sub[i] << "\n";
		cout << arr1[i] << " * " << arr2[i] << " = " << mul[i] << "\n";
		cout << arr1[i] << " / " << arr2[i] << " = " << mod[i] << "\n";
		cout << arr1[i] << " % " << arr2[i] << " = " << div[i] << "\n";
	}
	cout << "\nAdd\tSub\tMul\tDiv\tMod = \n";
	for(i = 0; i < size; i++)
	{
		cout << add[i] << "\t";
		cout << sub[i] << "\t";
		cout << mul[i] << "\t";
		cout << mod[i] << "\t";
		cout << div[i] << "\t\n";
	}

 	return 0;
}
Please Enter the Array Size =  2

Please Enter the First Array Items =  10 20

Please Enter the Second Array Items =  5 25

The Result of the 1 Iteration
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
10 % 5 = 0

The Result of the 2 Iteration
20 + 25 = 45
20 - 25 = -5
20 * 25 = 500
20 / 25 = 0
20 % 25 = 20

Add	Sub	Mul	Div	Mod = 
15	5	50	2	0	
45	-5	500	0	20