0

I have written this code to get array values from user and display them.

namespace Program_2
{

    class Program
    {
        static void Main(string[] args)
        {
            int[,] nums = new int[6,2];

            for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    Console.WriteLine("Enter Numbers");
                    nums[x, y] = int.Parse(Console.ReadLine());
                }
            }

            for(int j=0; j < 5; j++)
            {
                for(int k = 0; k < 2; k++)
                {
                    Console.Write(nums[j, k]+" ");
                }
                Console.WriteLine("");
            }

            Console.ReadLine();
        }
    }
}

But I want to know that how can I do Multiplication and Addition of arrays that was entered by the user.

Like this "See this image"

SHIVA
  • 11
  • 6

1 Answers1

0

SUM

int total = 0;
        // Iterate first dimension of the array
        for (int i = 0; i < nums.GetLength(0); i++)
        {
            // Iterate second dimension
            for (int j = 0; j < nums.GetLength(1); j++)
            {
                total += array[i, j];
            }
        }

Console.WriteLine(total);

MULTIFICATION

int total = 0;
        // Iterate first dimension of the array
        for (int i = 0; i < nums.GetLength(0); i++)
        {
            // Iterate second dimension
            for (int j = 0; j < nums.GetLength(1); j++)
            {
                total *= array[i, j];
            }
        }

Console.WriteLine(total);

BOTH IN ONE

int sum= 0;
int mul= 0;
            // Iterate first dimension of the array
            for (int i = 0; i < nums.GetLength(0); i++)
            {
                // Iterate second dimension
                for (int j = 0; j < nums.GetLength(1); j++)
                {
                    sum += array[i, j];
                    mul *= array[i, j];
                }
            }

    Console.WriteLine("SUM = " + sum);
    Console.WriteLine("MUL = " + mul);
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34