C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python Program to Convert Decimal to Binary, Octal and HexadecimalIn this tutorial, we will discuss how we can convert the decimal number into binary, octal, and hexadecimal numbers. Decimal System: The most widely used number system is decimal system. This system is base 10 number system. In this system, ten numbers (0-9) are used to represent a number. Binary System: Binary system is base 2 number system. Binary system is used because computers only understand binary numbers (0 and 1). Octal System: Octal system is base 8 number system. Hexadecimal System: Hexadecimal system is base 16 number system. This program is written to convert decimal to binary, octal and hexadecimal. We have a number in a decimal number, and we have to convert it into a binary, octal, and hexadecimal number. We will use a function for converting decimal numbers to binary numbers, decimal numbers to octal numbers, and decimal numbers to hexadecimal numbers. Examples: Input: 64 Output: 64 in Binary: 0b1000000 64 in Octal: 0o100 64 in Hexadecimal: 0x40 Input: 312 Output: 312 in Binary: 0b100111000 312 in Octal: 0o470 312 in Hexadecimal: 0x138 Code: # First, we will define the function to convert decimal to binary def decimal_into_binary(decimal_1): decimal = int(decimal_1) # then, print the equivalent decimal print ("The given decimal number", decimal, "in Binary number is: ", bin(decimal)) # we will define the function to convert decimal to octal def decimal_into_octal(decimal_1): decimal = int(decimal_1) # Then, print the equivalent decimal print ("The given decimal number", decimal, "in Octal number is: ", oct(decimal)) # we will define the function to convert decimal to hexadecimal def decimal_into_hexadecimal(decimal_1): decimal = int(decimal_1) # Then, print the equivalent decimal print ("The given decimal number", decimal, " in Hexadecimal number is: ", hex(decimal)) # Driver program decimal_1 = int (input (" Enter the Decimal Number: ")) decimal_into_binary(decimal_1) decimal_into_octal(decimal_1) decimal_into_hexadecimal(decimal_1) Output: Case - (1): Enter the Decimal Number: 12 The given decimal number 12 in Binary number is: 0b1100 The given decimal number 12 in Octal number is: 0o14 The given decimal number 12 in Hexadecimal number is: 0xc Case - (2): Enter the Decimal Number: 196 The given decimal number 196 in Binary number is: 0b11000100 The given decimal number 196 in Octal number is: 0o304 The given decimal number 196 in Hexadecimal number is: 0xc4 Explanation: In the above program, we have used the inbuilt functions: bin() (for binary), oct() (for octal), and hex() (for hexadecimal) for converting the given decimal number into respective number systems. These functions take an integer and return a string. ConclusionIn this tutorial, we have discussed how we can use the inbuilt functions of Python for converting decimal numbers into binary, octal, and hexadecimal number.
Next TopicPython ASCII value of a Character
|