Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Get only the odd values from a given array of integers

C# Sharp Array: Exercise-32 with Solution

Write a C# Sharp program to get only the odd values from a given array of integers.

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] nums = { 1, 2, 3, 5, 4, 2, 3, 4 };
            int[] result = test(nums);
            Console.WriteLine("Only the odd values of the said array:");
            Array.ForEach(result, Console.WriteLine);
            int[] nums1 = { 2, 4, 2, 6, 4, 8 };
            result = test(nums1);
            Console.WriteLine("Only the odd values of the said array:");
            Array.ForEach(result, Console.WriteLine);
        }
        public static int[] test(int[] arr)
        {
            return arr.Where(n => n % 2 != 0).ToArray();
        }
     }
}

Sample Output:

Only the odd values of the said array:
1
3
5
3
Only the odd values of the said array:
 

Flowchart:

Flowchart: Get only the odd values from a given array of integers.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to Check whether a Given Matrix is an Identity Matrix.
Next: Write a C# Sharp program to remove all duplicate elements from a given array and returns a new array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.