w3resource

C#: Multiply corresponding elements of two arrays of integers


Multiply Two Arrays

Write a C# program to multiply the corresponding elements of two integer arrays.

C# Sharp Exercises: Multiply corresponding elements of two arrays of integers

Sample Solution:-

C# Sharp Code:

using System;
using System.Collections.Generic;

public class Exercise31 {
    public static void Main() {
        // Declare and initialize the first array with integer values
        int[] first_array = {1, 3, -5, 4};

        // Declare and initialize the second array with integer values
        int[] second_array = {1, 4, -5, -2};

        // Display the elements of the first array
        Console.WriteLine("\nArray1: [{0}]", string.Join(", ", first_array));

        // Display the elements of the second array
        Console.WriteLine("Array2: [{0}]", string.Join(", ", second_array));

        // Display a message indicating multiplication of corresponding elements of both arrays
        Console.WriteLine("\nMultiply corresponding elements of two arrays: ");

        // Loop through the arrays to multiply corresponding elements and display the results
        for (int i = 0; i < first_array.Length; i++) {
            // Multiply the elements at index 'i' of both arrays and display the result
            Console.Write(first_array[i] * second_array[i] + " ");
        }

        // Move to the next line for a clean output format
        Console.WriteLine("\n");
    }
}

Sample Output:

Array1: [1, 3, -5, 4]                                                  
Array2: [1, 4, -5, -2]                                                 
                                                                       
Multiply corresponding elements of two arrays:                         
1 12 25 -8 

Flowchart:

Flowchart: C# Sharp Exercises - Multiply corresponding elements of two arrays of integers

For more Practice: Solve these Related Problems:

  • Write a C# program to multiply corresponding elements of two arrays and store only those whose result is even.
  • Write a C# program to multiply two arrays element-wise and check how many products are greater than 100.
  • Write a C# program to find the maximum product of corresponding elements of two arrays.
  • Write a C# program to multiply elements of two arrays and determine if the resulting array is in ascending order.

Go to:


PREV : Hexadecimal to Decimal.
NEXT : Four Copies of Last Four Characters.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.