C# Program to Count the Number of 1's in the Entered Numbers

This C# program demonstrates how to count the occurrences of a specific number (1) in an array of integers. We'll use an array to store the numbers and iterate through it to count how many times the value 1 appears.

Syntax

Following is the syntax for declaring an array and using a foreach loop to iterate through it −

int[] arrayName = new int[] {value1, value2, value3};

foreach(int variable in arrayName) {
   // process each element
}

Using Array and foreach Loop

The most straightforward approach is to use an array to store the numbers and a foreach loop to iterate through each element, checking if it equals 1 −

using System;

public class Demo {
   public static void Main() {
      int count = 0;
      int[] numbers = new int[] {1, 25, 1, 55, 1};
      
      Console.WriteLine("Numbers:");
      foreach (int num in numbers) {
         Console.WriteLine(num);
      }
      
      foreach (int num in numbers) {
         if (num == 1) {
            count++;
         }
      }
      
      Console.WriteLine("Number of 1's: " + count);
   }
}

The output of the above code is −

Numbers:
1
25
1
55
1
Number of 1's: 3

Using for Loop with Array

Alternatively, we can use a traditional for loop to iterate through the array by index −

using System;

public class Demo {
   public static void Main() {
      int count = 0;
      int[] numbers = new int[] {10, 1, 7, 1, 3, 1, 9};
      
      Console.WriteLine("Array contains:");
      for (int i = 0; i < numbers.Length; i++) {
         Console.Write(numbers[i] + " ");
         if (numbers[i] == 1) {
            count++;
         }
      }
      
      Console.WriteLine();
      Console.WriteLine("Total count of 1's: " + count);
   }
}

The output of the above code is −

Array contains:
10 1 7 1 3 1 9 
Total count of 1's: 3

Using LINQ Count Method

For a more concise solution, we can use LINQ's Count method to count elements that match a specific condition −

using System;
using System.Linq;

public class Demo {
   public static void Main() {
      int[] numbers = new int[] {1, 15, 1, 8, 1, 22, 1};
      
      Console.WriteLine("Numbers: " + string.Join(", ", numbers));
      
      int countOfOnes = numbers.Count(x => x == 1);
      
      Console.WriteLine("Number of 1's using LINQ: " + countOfOnes);
   }
}

The output of the above code is −

Numbers: 1, 15, 1, 8, 1, 22, 1
Number of 1's using LINQ: 4

Comparison of Approaches

Method Advantages Best Used When
foreach loop Simple, readable, no index needed You only need to check each element
for loop Access to index, more control You need element position information
LINQ Count Very concise, functional approach You want minimal code and use .NET 3.5+

Conclusion

Counting specific numbers in an array can be accomplished using foreach loops, for loops, or LINQ methods. The foreach loop approach is most readable for simple counting tasks, while LINQ provides the most concise solution for modern C# applications.

Updated on: 2026-03-17T07:04:35+05:30

690 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements