您好,欢迎访问一九零五行业门户网

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

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++; }}
examplethe 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(); }}
outputnumbers:1251551number of 1's: 3
以上就是c#程序:计算输入数字中1的个数的详细内容。
其它类似信息

推荐信息