আমি একটি অ্যারে ব্যবহার করে সংখ্যা যোগ করেছি −
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