Latest Post

Tuesday, 3 January 2017

How to print sum of 2 arrays in third array [C#]

Write a program in C# that inputs two arrays and saves sum of corresponding elements of these arrays in a third array and prints.

Following is the simple C# code :
class Lab
    {
        static void Main(string[] args)
        {
            int SIZE = 15;
            int[] firstArray = new int[SIZE];
            int[] secondArray = new int[SIZE];
            int[] thirdArray = new int[SIZE];
            int i, number;

            Console.WriteLine(" Enter the size of firstArray and secondArray \n");
            number = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(" Enter the elements of firstArray\n");
            for (i = 0; i < number; i++)
            {
                firstArray[i] = Convert.ToInt32(Console.ReadLine());

            }
            Console.WriteLine(" Enter the elements of secondArray\n");
            for (i = 0; i < number; i++)
            {
                secondArray[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("Sum of elements of  firstArray and secondArray\n");
            for (i = 0; i < number; i++)
            {
                thirdArray[i] = firstArray[i] + secondArray[i];
                Console.WriteLine("{0}\n", thirdArray[i]);
            }
            Console.ReadLine();
        }
    }

Output:-
Enter the size of firstArray and secondArray
3

Enter the elements of firstArray

1
2
3

Enter the elements of secondArray

4
5
6

Sum of elements of firstArray and secondArray

5
7
9


Government Jobs