w3resource

C#: Multiply all of elements of a given array of numbers by the array length


Multiply Array Elements by Length

Write a C# Sharp program to multiply all elements of a given array of numbers by array length.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            int[] nums = { 1, 3, 5, 7, 9 };

            // Call the test function and store the returned array in new_nums
            int[] new_nums = test(nums);

            // Print each element of the new_nums array using Array.ForEach and Console.WriteLine
            Array.ForEach(new_nums, Console.WriteLine);
        }

        // Function to multiply each element of the input array by the array length
        public static int[] test(int[] nums)
        {
            // Get the length of the input array
            var arr_len = nums.Length;

            // Using LINQ's Select method to multiply each element by the array length
            // and converting it back to an array using ToArray()
            return nums.Select(el => el * arr_len).ToArray();
        }
    }
}

Sample Output:

5
15
25
35
45

Flowchart:

Flowchart: C# Sharp Exercises - Multiply all of elements of a given array of numbers by the array length.

Sample Solution-1:

C# Sharp Code:

using System;
using System.Linq;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            int[] nums = { 1, 3, 5, 7, 9 };

            // Call the test function and store the returned array in new_nums
            int[] new_nums = test(nums);

            // Print each element of the new_nums array using Array.ForEach and Console.WriteLine
            Array.ForEach(new_nums, Console.WriteLine);
        }

        // Function to multiply each element of the input array by the array length
        public static int[] test(int[] nums)
        {
            // Loop through each element of the input array
            for (int i = 0; i < nums.Length; i++)
            {
                nums[i] *= nums.Length; // Multiply each element by the length of the array
            }

            return nums; // Return the modified array
        }
    }
}

Sample Output:

5
15
25
35
45

Flowchart:

Flowchart: C# Sharp Exercises - Multiply all of elements of a given array of numbers by the array length.

For more Practice: Solve these Related Problems:

  • Write a C# program to multiply each element of an array by its 1-based index position.
  • Write a C# program to multiply each element of an array by the number of digits in that element.
  • Write a C# program to multiply each element of an array by the count of even numbers in the array.
  • Write a C# program to multiply each odd element by array length and even element by index value.

Go to:


PREV : Get File Name from Path.
NEXT : Min Value from Two String Numbers.

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.