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: Count a specified number in a given array of integers

C# Sharp Basic: Exercise-45 with Solution

Write a C# program to count a specified number in a given array of integers.

Pictorial Presentation:

>C# Sharp Exercises: Count a specified number in a given array of integers

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)
    {
            Console.WriteLine("\nInput an integer:");  
            int x = Convert.ToInt32(Console.ReadLine());
            int[] nums = {1, 2, 2, 3, 3, 4, 5, 6, 5, 7, 7, 7, 8, 8, 9};
            Console.WriteLine("Number of " + x + " present in the said array:");
            Console.WriteLine(nums.Count(n => n == x));
    }
}

Sample Output:

Input an integer:                                                      
5                                                                      
Number of 5 present in the said array:                                 
2

Flowchart:

Flowchart: C# Sharp Exercises - Count a specified number in a given array of integers

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to create a new string of every other character (odd position) from the first position of a given string.
Next: Write a C# program to check if a number appears as either the first or last element of an array of integers and the length is 1 or more.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.