Program to convert Decimal to Binary, Octal and Hexadecimal in Python

Program to convert Decimal to Binary, Octal and Hexadecimal in Python. This programs contains functions, hexa-decimal, octal, binary in-built functions, input and print statement. This program is for beginners, class 11,12 and B Tech computer science students.

Program to convert Decimal to Binary, Octal and Hexadecimal in Python :

What is Binary, Octal and Hexa-decimal ?

Binary (or base-2) a numeric system that only uses two digits — 0 and 1. Computers operate in binary, meaning they store data and perform calculations using only zeros and ones. A single binary digit can only represent True (1) or False (0) in boolean logic.
Octal Number System has a base of eight and uses the number from 0 to 7. The octal numbers, in the number system, are usually represented by binary numbers when they are grouped in pairs of three.  For example, 128 is expressed as 0010102, where 1 is equivalent to 001 and 2 is equivalent to 010.
Hexadecimal The hexadecimal number system is a type of number system, that has a base value equal to 16. It is also pronounced sometimes as ‘hex’. Hexadecimal numbers are represented by only 16 symbols. These symbols or values are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F.

Source code : Program to convert Decimal to Binary, Octal and Hexadecimal in Python


def decimal_into_binary(decimal_1):  
    decimal = int(decimal_1)  
    
      
    print ("The given decimal number", decimal, "in Binary number is: ", bin(decimal))  
  
def decimal_into_octal(decimal_1):  
    decimal = int(decimal_1)  
    
      
    print ("The given decimal number", decimal, "in Octal number is: ", oct(decimal))  
  
def decimal_into_hexadecimal(decimal_1):  
    decimal = int(decimal_1)  
    
     
    print ("The given decimal number", decimal, " in Hexadecimal number is: ", hex(decimal))  
    
  
decimal_1 = int (input (" Enter the Decimal Number: "))  
decimal_into_binary(decimal_1)  
decimal_into_octal(decimal_1)  
decimal_into_hexadecimal(decimal_1)

Download Source code : For Download Click on the below Button

Step wise Source code explanation

Step 1 : First, we will define the function to convert decimal to binary
def decimal_into_binary(decimal_1):
decimal = int(decimal_1)
Step 2 : Then, print the equivalent decimal
print (“The given decimal number”, decimal, “in Binary number is: “, bin(decimal))
Step 3 : we will define the function to convert decimal to octal
def decimal_into_octal(decimal_1):
decimal = int(decimal_1)
Step 4 : Then, print the equivalent Octal
print (“The given decimal number”, decimal, “in Octal number is: “, oct(decimal))
Step 5 : We will define the function to convert decimal to hexadecimal
def decimal_into_hexadecimal(decimal_1):
decimal = int(decimal_1)
Step 6 : Then, print the equivalent Hexa-decimal
print (“The given decimal number”, decimal, ” in Hexadecimal number is: “, hex(decimal))
Step 7: Input capture and argument feeding of function and calling functions
decimal_1 = int (input (” Enter the Decimal Number: “))
decimal_into_binary(decimal_1)
decimal_into_octal(decimal_1)
decimal_into_hexadecimal(decimal_1)

Output:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
 Enter the Decimal Number: 56
The given decimal number 56 in Binary number is:  0b111000
The given decimal number 56 in Octal number is:  0o70
The given decimal number 56  in Hexadecimal number is:  0x38
>>> 
 RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py 
 Enter the Decimal Number: 36
The given decimal number 36 in Binary number is:  0b100100
The given decimal number 36 in Octal number is:  0o44
The given decimal number 36  in Hexadecimal number is:  0x24
>>> 

Output Explanation :

First the decimal is asked
Enter the Decimal Number: 56
Conversion and printing happens:
The given decimal number 56 in Binary number is: 0b111000
The given decimal number 56 in Octal number is: 0o70
The given decimal number 56 in Hexadecimal number is: 0x38

Conclusion :

The program is running fine.

Thank you for reading and learning from Program to convert Decimal to Binary, Octal and Hexadecimal in Python post. This programs contains function, hexa-decimal, octal, binary in-built function,input and print statement. This program is for beginners, class 11,12 and B Tech computer science students. For program practice below link is provided.
Python Programs Archives – CS Study
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study

Leave a Comment

Your email address will not be published.