С C++ на C#: определение количества нулей среди всех цифр числа
Формулировка задачи:
Дано натуральное число n. Напишите функцию static int NumberOfZeroes (int n),
определяющую количество нулей среди всех цифр числа n. Выведите результат.
#include "iostream"
using namespace std;
void zero(char *mas, int len)// твоя NumberOfZeroes
{
int count=0;
for (int i=0; i<len; i++)
if (mas[i]=='0')
count++;
cout<<"Количество нулей в числе = "<<count<<endl;
}
int main()
{
setlocale (0,"");
char mas[100];
int len;
cout<<"Введите число: ";
cin>>mas;
cout<<"Ваше число: "<<mas<<endl;
len=strlen(mas);
cout<<"Длина числа: "<<len<<endl;;
zero(mas,len);
system ("pause");
return 0;
}Решение задачи: «С C++ на C#: определение количества нулей среди всех цифр числа»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void zero(char[] mas, int len)// твоя NumberOfZeroes
{
int count = 0;
for (int i = 0; i < len; i++)
if (mas[i] == '0')
count++;
Console.WriteLine("Количество нулей в числе = " + count);
}
static void Main(string[] args)
{
string mas = null;
Console.WriteLine("Введите число: ");
mas = Console.ReadLine();
Console.WriteLine("Ваше число: " + mas);
Console.WriteLine("Длина числа: " + mas.Length);
zero(mas.ToCharArray(), mas.Length);
}
}
}