Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Check if a given number present in an array of numbers

C# Sharp Basic: Exercise-63 with Solution

Write a C# program to check if a given number present in an array of numbers.

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] nums = { 1, 3, 5, 7, 9 };
            int n = 6;
            Console.WriteLine(test(nums, n));
            n = 3;
            Console.WriteLine(test(nums, n));
        }
        public static bool test(int[] arra, int n)
        {
            return arra.Contains(n);
        }
    }
}

Sample Output:

False
True

Flowchart:

Flowchart: C# Sharp Exercises - Check if a given number present in an array of numbers.

Sample Solution-1:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] nums = { 1, 3, 5, 7, 9 };
            int n = 6;
            Console.WriteLine(test(nums, n));
            n = 3;
            Console.WriteLine(test(nums, n));
        }
        public static bool test(int[] arra, int n)
        {
            foreach (int item in arra)
            {
                if (item == n) 
                    return true;
            }
            return false;
        }
    }
}

Sample Output:

False
True

Flowchart:

Flowchart: C# Sharp Exercises - Check if a given number present in an array of numbers.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to reverse the strings contained in each pair of matching parentheses in a given string and also remove the parentheses within the given string.
Next: Write a C# Sharp program to get the file name (including extension) from a given path.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.