In this C# program, we will learn how to write a program to count the number of 1's present in the array.
Here is the code of the program to count the number of 1's present in the array:
using System;
namespace Tutorialsrack
{
class Program
{
/* C# Program to Count the Number of 1's Present in the Array */
static void Main(string[] args)
{
int size, count = 0;
Console.Write("Enter the size of the array: ");
size = int.Parse(Console.ReadLine());
int[] a = new int[size];
Console.Write("Enter the Numbers:\n");
for (int i = 0; i < size; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
foreach (int o in a)
{
if (o == 1)
{
count++;
}
}
Console.Write("\nNumber of 1's Present in the Array: {0}", count);
Console.ReadKey();
}
}
}
Enter the size of the array: 5
Enter the Numbers:
1
1
2
5
6
Number of 1's Present in the Array: 2
Comments