- Ask the user for the array size and to input the number of elements in the list
- Ask for a value “n” from the user
- Find the sum of the first “n” odd-valued elements
4.Display The sum
For example: If the array is {1,2,3,4,5,6,7,8,9,10} and the value of “n” is 3, the sum of the first three odd positions (1,3,5) is 12.*
Hi,
(1,3,5) is 12.*
Isn’t the result 9? Is this 0 base index?
And do you mean value is odd or position of the value?
Anyway, the following will help you.
Value is odd.
arrInt.Where(Function(x) x mod 2 =1).Take(n).Sum()
Position is odd (1 base index)
arrInt.Where(Function(x,i) i mod 2 =0).Take(n).Sum()
Regards,
Oh yes, the addition will be 9. I meant the value is odd.
If you can help me out without using Lamda Function. I am new to it.
Hi,
Can you try to modify Sum(userNumber)
to Sum()
?
Regards,
Thankyou, my mistake. Both the code worked. Can you refer me any link where I can learn Lambda functions.
Hi,
For now, the following topic will help you.
Regards,
Thankyou so much @Yoichi
@Yoichi .What will be the flow if i have to look for odd positions and not odd numbers, Not using Lambda Function.
Hi,
Can you try to add index variable in index property of ForEach and modify condition as the following?
Note: index of ForEach is 0-based. So even number of the index is odd number as we count it 1 based.
Sequence3.xaml (8.1 KB)
Regards,
It worked ,and I understood the logic as well.Thankyou
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.