C#: Count a specified number in a given array of integers
Count Specific Number in Array
Write a C# program to count a specified number in a given array of integers.
Pictorial Presentation:

Sample Solution:
C# Sharp Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Exercise45 {
static void Main(string[] args) {
// Prompt the user to input an integer
Console.WriteLine("\nInput an integer:");
// Read the input integer and store it in the variable 'x'
int x = Convert.ToInt32(Console.ReadLine());
// Define an array of integers 'nums' with pre-defined values
int[] nums = {1, 2, 2, 3, 3, 4, 5, 6, 5, 7, 7, 7, 8, 8, 9};
// Display a message indicating the number being searched for in the array
Console.WriteLine("Number of " + x + " present in the said array:");
// Count the occurrences of the input integer 'x' in the 'nums' array and print the count
Console.WriteLine(nums.Count(n => n == x));
}
}
Sample Output:
Input an integer: 5 Number of 5 present in the said array: 2
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C# program to find the number that appears most frequently in an array.
- Write a C# program to count how many even numbers appear in an array.
- Write a C# program to find the total number of occurrences of multiple user-specified numbers.
- Write a C# program to determine the count of duplicates in an array.
Go to:
PREV : Every Other Character in String.
NEXT : Check Number First or Last in Array.
C# Sharp Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.