Write a lambda expression to add all the natural numbers below five hundred that are multiples of 7 or 11.

 Posted by Niladri.Biswas on 7/30/2012 | Category: C# Interview questions | Views: 1603 | Points: 40

var ans =  Enumerable.Range(0, 500).Where(i => i % 7 == 0 || i % 11 == 0).Sum();

Enumerable.Range generates a sequence of integral numbers within a specified range.
In this case it is between 0 to 500. After that inside the Where extension method we are checking if the number is divisible by 7 as well as 11 or not.Which ever number satisfies the condition, we are taking a sum of that.

Result: 27660

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response