बाइनरी को डेसीमल में कन्वर्ट करने के लिए, मैंने यहां व्हाइल लूप का उपयोग किया है और बाइनरी नंबर का शेषफल निकाला है, जो इनपुट है। उसके बाद, शेषफल को बेस वैल्यू से गुणा किया जाता है और जोड़ा जाता है।
डेसीमल वैल्यू प्राप्त करने के लिए मैंने यह किया:
while (val > 0) { remainder = val % 10; myDecimal = myDecimal + remainder * baseVal; val = val / 10; baseVal = baseVal * 2; }उदाहरण
C# में बाइनरी को डेसीमल में कन्वर्ट करने का पूर्ण कोड:
using System; using System.Collections.Generic; using System.Text; namespace Demo { class toBinary { static void Main(string[] args) { int val = 1010, myBinary, remainder; int myDecimal = 0, baseVal = 1; myBinary = val; while (val > 0) { remainder = val % 10; myDecimal = myDecimal + remainder * baseVal; val = val / 10; baseVal = baseVal * 2; } Console.Write("Binary Number : " + myBinary); Console.Write("\nConverted to Decimal: " + myDecimal); Console.ReadLine(); } } }आउटपुट
Binary Number : 1010 Converted to Decimal: 10