- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Count the Number of 1's in the Entered Numbers
I have added the numbers using an array −
int[] num = new int[] {1, 25, 1, 55, 1};
Now loop through and find for 1, if 1 is there, 6 then increment the variable that counts the occurrence −
foreach(int j in num) { if (j == 1) { cal++; } }
Example
The following is the code to count the number of 1’s in the entered number.
using System; public class Demo { public static void Main() { int cal = 0; int[] num = new int[] {1, 25, 1, 55, 1}; Console.WriteLine("Numbers:"); for (int i = 0; i < 5; i++) { Console.WriteLine(num[i]); } foreach (int j in num) { if (j == 1) { cal++; } } Console.WriteLine("Number of 1's: "); Console.WriteLine(cal); Console.ReadLine(); } }
Output
Numbers: 1 25 1 55 1 Number of 1's: 3
- Related Articles
- Count number of binary strings without consecutive 1's in C
- Maximum sub-matrix area having count of 1's one more than count of 0’s in C++ program
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- C# Program to display the Factors of the Entered Number
- Count Numbers with N digits which consists of even number of 0's in C++
- Count Numbers with N digits which consists of odd number of 0's in C++
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1's and 2's complement of 16-bit number
- C# program to display factors of entered number
- Python Program to Compute the Value of Euler's Number e. Use the Formula: e = 1 + 1/1! + 1/2! + …… 1/n!
- Count subarrays with equal number of 1’s and 0’s in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Find the index of first 1 in a sorted array of 0's and 1's in C++
- Python program using the map function to find a row with the maximum number of 1's

Advertisements