Skip to main content

Welcome to Coding 4 Programming

Addition of Two Binary Numbers | Function | Python

CONVERSION OF DECIMAL NUMBER TO BINARY, OCTAL AND HEXADECIMAL NUMBER SYSTEM | PYTHON

Python Program of Conversion of Decimal Number to Binary, Octal and Hexadecimal Number System by simple method through user define function.















  1. def binf(n):
  2.     b=0
  3.     i=0
  4.     while(n!=0):
  5.         r=n%2
  6.         b+=r*(10**i)
  7.         n=n//2
  8.         i+=1
  9.     return b
  10. def octf(n):
  11.     o=0
  12.     i=0
  13.     while(n!=0):
  14.         r=n%8
  15.         o+=r*(10**i)
  16.         n=n//8
  17.         i+=1
  18.     return o
  19. def hxdf(n):
  20.     h=[]
  21.     while(n!=0):  
  22.         r=n%16
  23.         if(r<10):
  24.             h.append(r)
  25.         if(r==10):
  26.             h.append("A")
  27.         if(r==11):
  28.             h.append("B")
  29.         if(r==12):
  30.             h.append("C")
  31.         if(r==13):
  32.             h.append("D")
  33.         if(r==14):
  34.             h.append("E")
  35.         if(r==15):
  36.             h.append("F")
  37.         n=n//16
  38.     nl=[]
  39.     for i in range(len(h)-1,-1,-1):
  40.         nl.append(h[i])
  41.     return nl
  42. n=int(input("Enter Decimal Number="))
  43. bn=binf(n)
  44. print("Binary Number of",n,"is",bn)
  45. on=octf(n)
  46. print("Octal Number of",n,"is",on)
  47. hn1=hxdf(n)
  48. ltostr=' '.join(map(str, hn1))
  49. print("Hexadecimal Number of",n,"is",ltostr)

Output Screen:

Number System
Number System

Note:


  • If you have any problem related to this program. Please do comment your Problem or Mail us.
  • For illustrating the any concept you can comment by line number.
  • Thank you! For more programs do regular visit to Coding 4 Programming.

Comments

Popular Posts