मैंने एक सरणी का उपयोग करके संख्याओं को जोड़ा है -
int[] num = new int[] {1, 25, 1, 55, 1}; अब लूप करें और 1 के लिए खोजें, यदि 1 है, तो 6 तो घटना की गणना करने वाले वेरिएबल को बढ़ाएँ -
foreach(int j in num) {
if (j == 1) {
cal++;
}
} उदाहरण
दर्ज की गई संख्या में 1 की संख्या गिनने के लिए निम्नलिखित कोड है।
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();
}
} आउटपुट
Numbers: 1 25 1 55 1 Number of 1's: 3