Перевод из десятичной системы счисления в двоичную - C# (194753)
Формулировка задачи:
Помогите пожалуйста довести код до рабочего состояния. Что здесь нужно дописать?
class Program
{
static void perevod(int x)
{
if (x >= 2)
{
perevod(x / 2);
Console.Write(x % 2);
}
else Console.Write(x);
}
static void Main(string[] args)
{
Console.WriteLine("Введите X ");
int x = int.Parse(Console.ReadLine());
int k = perevod(x);
if (x >= 0)
{
Console.WriteLine(k);
}
else
Console.WriteLine("данные не корректны");
Console.ReadKey();
}
}
}Решение задачи: «Перевод из десятичной системы счисления в двоичную»
textual
Листинг программы
static int Perevod(int x)
{
return x < 2 ? x % 2 : (x % 2) + 10 * Perevod(x / 2);
}
static void Main(string[] args)
{
Console.WriteLine("Введите X ");
int x = int.Parse(Console.ReadLine());
int k = Perevod(x);
if (x >= 0)
{
Console.WriteLine(k);
}
else
{
Console.WriteLine("данные не корректны");
}
Console.ReadKey();
}