I have been messing around with Lambda expressions in C#, teaching myself, and trying to test myself here. The problem with my code is not in evaluating the array for these set conditions, but rather I am having a difficult time capturing user input to build out the array. Have tried a few different methods, some mentioned here, but seem to have trouble adapting them to my code. Also any input on simplifying my expressions for checking if it has odd only or even only would be appreciated! I feel they are slightly bloated. Thanks!
using System;
namespace BuildingArrays
{
class Program
{
static void Main(string[] args)
{
int[] numbers = {Int32.TryParse(string, Console.ReadLine())};
bool hasOddOnly = Array.Exists(numbers, (int num) =>
{
bool hasEven = num % 2 == 0;
return hasEven;
});
hasOddOnly = !hasOddOnly;
bool hasEvenOnly = Array.Exists(numbers, (int num) =>
{
bool hasOdd = num % 2 != 0;
return hasOdd;
});
hasEvenOnly = !hasEvenOnly;
bool hasOddAnd4 = Array.Exists(numbers, (int num) =>
{
bool hasOdd = num % 2 != 0;
bool is4 = num == 4;
return is4 && hasOdd;
});
bool multipleOf4 = Array.Exists(numbers, (int num)=>
{
bool multiple = num % 4 == 0;
bool multiple2 = num % 3 == 0;
return multiple || multiple2;
});
bool multipleOf4and3 = Array.Exists(numbers, (int num)=>
{
bool multipleBoth = num % 4 == 0 && num % 3 == 0;
return multipleBoth;
});
Console.WriteLine($"This array contains odd numbers and 4 is {hasOddAnd4}");
Console.WriteLine($"This array contains ONLY odd numbers is {hasOddOnly}.");
Console.WriteLine($"This array contains ONLY even numbers is {hasEvenOnly}.");
Console.WriteLine($"This array contains either a multiple of 4, or a multiple of 3 is {multipleOf4}.");
Console.WriteLine($"This array contains a number which is both a multiple of 4 and 3 is {multipleOf4and3}");
}
}
}