w3resource

C#: Check specific digit in an array of numbers

C# Sharp Array: Exercise-38 with Solution

Write a C# Sharp program that takes an array of numbers and a digit. Check whether the digit is present in this array of numbers.

Sample Data:
({7, 5, 85, 9, 11, 23, 18}, "1") - > "Present"
({7, 5, 85, 9, 11, 23, 18}, "1") - > "Present"
({7, 5, 85, 9, 11, 23, 18}, "1") - > "Not present..!"

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
   class Program
   {
       static void Main(string[] args)
       {
           int[] nums = { 7, 5, 85, 9, 11, 23, 18 };
           string n = "1";
           Console.WriteLine("Original array elements:");
           Console.WriteLine($"{string.Join(", ", nums)}");
           Console.WriteLine("\nDigit to search: " + n);
           Console.WriteLine("Check the said digit present in the array!: " +test(nums, n));
           n = "9";
           Console.WriteLine("\nDigit to search: " + n);
           Console.WriteLine("Check the said digit present in the array!: " + test(nums, n));
           n = "4";
           Console.WriteLine("\nDigit to search: " + n);
           Console.WriteLine("Check the said digit present in the array!: " + test(nums, n));
       }
       public static string test(int[] nums, string n)
       {
           return nums.Select(s => $"{s}").Any(a => a.Contains(n)) ? "Present" : "Not present..!";
        }
   }
}

Sample Output:

Original array elements:
7, 5, 85, 9, 11, 23, 18

Digit to search: 1
Check the said digit present in the array!: Present

Digit to search: 9
Check the said digit present in the array!: Present

Digit to search: 4
Check the said digit present in the array!: Not present..!
 

Flowchart:

Flowchart:  Check specific digit in an array of numbers.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous C# Sharp Exercise: Smallest gap between the numbers in an array.
Next C# Sharp Exercise: Sum of all prime numbers in an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook