Sunday, 24 December 2017

Question :
Write a function named lastEven that returns the index of the last even value in its array argument. For example, lastEven will return 3 if the array is {3, 2, 5, 6, 7}, because that is the index of 6 which is the last even value in the array.

If the array has no even numbers, the function should return -1.

If you are programming in Java or C#, the function signature is
   int lastEven (int[ ] a)

If you are programming in C or C++, the function signature is

   int lastEven (int a[ ], int len) where len is the number of elements in a.
Solution :
  public static int lastEven(int [] a)
        {
            int lastIndex = 0;
            for (int i = a.Length - 1; i > 0;i-- )
            {
                if (a[i] % 2 == 0)
                {
                    lastIndex = i;
                    break;
                }
            }
            return lastIndex;
        }

2 comments:

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page