दशमलव का बाइनरी प्राप्त करने के लिए, रिकर्सन का उपयोग करके, सबसे पहले दशमलव संख्या सेट करें -
int dec = 30;
अब मान को किसी फ़ंक्शन में पास करें -
public int displayBinary(int dec) {
} अब, स्थिति की जाँच करें जब तक कि दशमलव मान 0 न हो और रिकर्सन का उपयोग करके दशमलव संख्या का मॉड 2 प्राप्त करें जैसा कि नीचे दिखाया गया है। पुनरावर्ती कॉल फ़ंक्शन को dec/2 मान के साथ फिर से कॉल करेगा -
public int displayBinary(int dec) {
int res;
if (dec != 0) {
res = (dec % 2) + 10 * displayBinary(dec / 2);
Console.Write(res);
return 0;
} else {
return 0;
}
} निम्नलिखित पूरा कोड है -
उदाहरण
using System;
public class Program {
public static void Main(string[] args) {
int dec;
Demo d = new Demo();
dec = 30;
Console.Write("Decimal = "+dec);
Console.Write("\nBinary of {0} = ", dec);
d.displayBinary (dec);
Console.ReadLine();
Console.Write("\n");
}
}
public class Demo {
public int displayBinary(int dec){
int res;
if (dec != 0) {
res = (dec % 2) + 10 * displayBinary(dec / 2);
Console.Write(res);
return 0;
} else {
return 0;
}
}
} आउटपुट
Decimal = 30 Binary of 30 = 11110