C++ Program to Add Two Arrays

Write a C++ Program to Add Two Arrays with an example. In this C++ addition of two arrays example, we allow the user to enter the array size and array items. Next, we used the C++ for loop to iterate the array from 0 to size.

Within the for loop, we added both the array items and assigned them to a new array called add. Here, we also used the cout statement to show you the result at each for loop iteration. Finally, we used one more for loop to print the add array items.

#include<iostream>
using namespace std;

int main()
{
	int size, i, arr1[10], arr2[10], add[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];
		cout << arr1[i] << " + " << arr2[i] << " = " << add[i] << "\n";
	}
	cout << "\nThe Final Result of adding 2 One Dimensional Arrays = ";
	for(i = 0; i < size; i++)
	{
		cout << add[i] << " ";
	}

 	return 0;
}
C++ Program to Add Two Arrays 1

In this C++ add arrays example, we removed that extra for loop to display the items and placed a cout statement after performing the addition.

#include<iostream>
using namespace std;

int main()
{
	int size, i, arr1[10], arr2[10], add[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];
	}
	cout << "\nThe Final Result of adding 2 One Dimensional Arrays = ";
	for(i = 0; i < size; i++)
	{
		add[i] = arr1[i] + arr2[i];
		cout << add[i] << " ";
	}

 	return 0;
}
Please Enter the Array Size =  5

Please Enter the First Array Items =  10 20 30 40 50

Please Enter the Second Array Items =  9 29 35 55 77

The Final Result of adding 2 One Dimensional Arrays = 19 49 65 95 127 

C++ Program to Add Two Arrays using a While Loop

#include<iostream>
using namespace std;

int main()
{
	int size, i, arr1[10], arr2[10], add[10];
	
	cout << "\nPlease Enter the Array Size =  ";
	cin >> size;
	
	cout << "\nPlease Enter the First Array Items =  ";
	i = 0; 
	while(i < size)
	{
		cin >> arr1[i];
		i++;
	}	
	cout << "\nPlease Enter the Second Array Items =  ";
	i = 0; 
	while(i < size)
	{
		cin >> arr2[i];
		i++;
	}
	cout << "\nThe Final Result of adding 2 One Dimensional Arrays = ";
	i = 0; 
	while(i < size)
	{
		add[i] = arr1[i] + arr2[i];
		cout << add[i] << " ";
		i++;
	}

 	return 0;
}
Please Enter the Array Size =  7

Please Enter the First Array Items =  1 2 3 4 5 6 7

Please Enter the Second Array Items =  10 11 12 13 14 15 16

The Final Result of adding 2 One Dimensional Arrays = 11 13 15 17 19 21 23