दशमलव संख्या और बाइनरी संख्या दी गई है, हमारा कार्य दशमलव को बाइनरी संख्या में और बाइनरी को दशमलव संख्या में बदलना है।
एल्गोरिदम
Bintodec()
चरण 1: बाइनरी संख्या दर्ज करें। चरण 2: अगला बाइनरी संख्या की लंबाई लें। चरण 3: फॉर लूप का उपयोग करके बाइनरी को दशमलव संख्या में बदलें। जैसे यदि बाइनरी संख्या 1111 है, तो गणना होगी 1*2**3+1*2**2+1*2**1+1*2**0 = 15 चरण 4: संख्या प्रदर्शित करें।
Dectobin()
चरण 1: दशमलव संख्या दर्ज करें। चरण 2: व्हाइल लूप का उपयोग करके *संख्या को 2 से विभाजित करें। शेषफल और भागफल दोनों खोजें। एक और वेरिएबल लें जिसे 1 से आरंभ किया गया हो। अब शेषफल को इस वेरिएबल से गुणा किया जाएगा और अंतिम आउटपुट संख्या में जोड़ा जाएगा। वह वेरिएबल 1 से बढ़ाया जाएगा। *पहला शेषफल अनुक्रम में अंतिम अंक है। चरण 3: मान प्रदर्शित करें।
उदाहरण कोड
print("*****************************************************")
print(" DECIMAL TO BINARY AND BINARY TO DECIMAL CONVERSION")
print("*****************************************************")
print(" For Decimal to Binary...Press 1.")
print(" For Binary to Decimal... Press 2")
print("*****************************************************")
my_choice = int(input("Enter your choice: "))
if my_choice == 1:
i = 1
s = 0
my_dec = int(input("Enter decimal to be converted: "))
while my_dec > 0:
rem = int(my_dec % 2)
s = s + (i * rem)
my_dec = int(my_dec / 2)
i = i * 10
print("The binary of the given number is ", s, '.')
else:
my_bin = input('Enter binary to be converted: ')
n = len(my_bin)
res = 0
for i in range(1, n + 1):
res = res + int(my_bin[i - 1]) * 2 ** (n - i)
print("The decimal of the given binary is ", res, '.')
print("******************************************************")
आउटपुट
***************************************************** DECIMAL TO BINARY AND BINARY TO DECIMAL CONVERSION ***************************************************** For Decimal to Binary...Press 1. For Binary to Decimal... Press 2 ***************************************************** Enter your choice: 1 Enter decimal to be converted: 15 The binary of the given number is 1111. ****************************************************** ***************************************************** DECIMAL TO BINARY AND BINARY TO DECIMAL CONVERSION ***************************************************** For Decimal to Binary...Press 1. For Binary to Decimal... Press 2 ***************************************************** Enter your choice: 2 Enter binary to be converted: 1111 The decimal of the given binary is 15. ******************************************************