Saturday, 8 April 2017

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

  1. *
  2.  * C# Program to Count the Number of 1's in the Entered Number
  3.  */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace ConsoleApplication16
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int m, count = 0;
  16.             Console.WriteLine("Enter the Limit : ");
  17.             m = int.Parse(Console.ReadLine());
  18.             int[] a = new int[m];
  19.             Console.WriteLine("Enter the Numbers :");
  20.             for (int i = 0; i < m; i++)
  21.             {
  22.                 a[i] = Convert.ToInt32(Console.ReadLine());
  23.             }
  24.             foreach (int o in a)
  25.             {
  26.                 if (o == 1)
  27.                 {
  28.                     count++;
  29.                 }
  30.             }
  31.             Console.WriteLine("Number of 1's in the Entered Number : ");
  32.             Console.WriteLine(count);
  33.             Console.ReadLine();
  34.         }
  35.     }
  36. }
Here is the output of the C# Program:
Enter the Limit : 5
Enter the Numbers : 
1
2
1
4
1
Number of 1's in the Entered Number : 3

No comments:

Post a Comment