Q:

Write a C# Sharp program to check if a given array of integers contains 5 next to a 5 somewhere

0

Write a C# Sharp program to check if a given array of integers contains 5 next to a 5 somewhere

Sample Output:

False
True
True

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {               
         Console.WriteLine(test(new[] { 1, 5, 6, 9, 10, 17 })); 
         Console.WriteLine(test(new[] { 1, 5, 5, 9, 10, 17 })); 
         Console.WriteLine(test(new[] { 1, 5, 5, 9, 10, 17, 5, 5 })); 
        }           
       static bool test(int[] nums)
         {
            for (int i = 0; i < nums.Length - 1; i++)
             {
                if (nums[i] == 5 && nums[i] == nums[i + 1]) return true;
              }

            return false;
          }
   } 
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now