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:
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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework