Reputation: 143
I want my program to read user input and save it into an array, and then sum all the numbers entered by user, and select those, which divide the sum. Is that possible to use any of the ready c# methods to do it (like FindAll() ?)
Final version of my program should list all numbers that divide the sum of all entered numbers.
My code is following :
Console.WriteLine ("Please type 10 numbers");
int[] numbers = new int[10];
int sum = 0;
for (int i = 0; i < numbers.Length; i++) {
string input = Console.ReadLine ();
int.TryParse (input, out numbers[i]);
sum += numbers [i];
}
Console.WriteLine (sum);
Upvotes: 0
Views: 296
Reputation: 4624
If you can use LINQ you can do something like:
int[] dividers = numbers.Where(number => number % sum == 0).ToArray();
Upvotes: 1
Reputation: 109557
Nothing built in, but you can do it like this:
int[] data = new [] {2, 3, 4, 5, 6};
int sum = data.Sum();
var dividers = from number in data
where (sum % number) == 0
select number;
foreach (var divider in dividers)
Console.WriteLine(divider);
Upvotes: 1