n 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.
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:
Output:
Case - (1):
Case - (2):
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.
Conclusion
In this tutorial, we have discussed how we can use the inbuilt functions of Python for converting decimal numbers into binary, octal, and hexadecimal number.
need an explanation for this answer? contact us directly to get an explanation for this answer