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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework