- Open Visual Studio 2012
- "File" -> "New" -> "Project..."
- Choose "Template" -> "Visual C#" -> "Console application"
Perform Addition
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 10, 20, 30, 40, 50 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] arr = new int[n];
for (int i = 0, j = 0; i < a.Length; i++, j++)
{
arr[i] = a[i] + b[j];
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.Read();
}
}
}
Output
![sum-of-array.jpg]()
Note: In the code shown above the sum of two single-dimension arrays is produced. Which is done simply by the "+" operator.
Perform Subtraction
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 10, 20, 30, 40, 50 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] arr = new int[n];
for (int i = 0, j = 0; i < a.Length; i++, j++)
{
arr[i] = a[i] - b[j];
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.Read();
}
Output
![subtraction-of-array.jpg]()
Perform Multiplication
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 10, 20, 30, 40, 50 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] arr = new int[n];
for (int i = 0, j = 0; i < a.Length; i++, j++)
{
arr[i] = a[i] * b[j];
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.Read();
}
Output
![multiplication-of-array.jpg]()
Perform Division
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 10, 20, 30, 40, 50 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] arr = new int[n];
for (int i = 0, j = 0; i < a.Length; i++, j++)
{
arr[i] = a[i] / b[j];
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.Read();
}
Output
![division-of-array.jpg]()
Summary
In this article I have described all the arithmetic operations on a single-dimension array using the same methods in a simple way.