Write a program in C# Sharp to convert a decimal number to binary using recursion

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to convert a decimal number to binary using recursion. 
Test Data :
Input a decimal number : 66
Expected Output :
The binary equivalent of 66 is : 1000010

الأجوبة

using System;
class RecExercise13
{
    public static void Main(string[] args)
    {
        int num;
        DecToBinClass pg = new DecToBinClass();
		Console.WriteLine("\n\n Recursion : Convert a decimal number to binary :");
		Console.WriteLine("------------------------------------------------------"); 
        Console.Write(" Input a decimal number : ");
        num = int.Parse(Console.ReadLine());
        Console.Write(" The binary equivalent of {0} is : ", num);
        pg.deciToBinary (num);
        Console.ReadLine();
        Console.Write("\n");
    }
}
public class DecToBinClass
{
    public int deciToBinary(int num)
    {
        int bin;
        if (num != 0)
        {
            bin = (num % 2) + 10 * deciToBinary(num / 2);
            Console.Write(bin);
            return 0;
        }
        else
        {
            return 0;
        } 
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...