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:

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.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
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