C#程序:计算输入数字中1的个数

2023年 8月 27日 60.9k 0

C#程序:计算输入数字中1的个数

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.

Live Demo

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

登录后复制

以上就是C#程序:计算输入数字中1的个数的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论