Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal.

Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal.

How can we manually convert a decimal number to binary, octal and hexadecimal?
Converting a decimal number to binary, octal, and hexadecimal involves dividing the decimal number by the base repeatedly and noting the remainders at each step. Here's a simple example:
Let's convert the decimal number 27 to binary, octal, and hexadecimal.

1.Binary:
Divide 27 by 2. Quotient is 13, remainder is 1. Note the remainder.
Divide 13 by 2. Quotient is 6, remainder is 1. Note the remainder.
Divide 6 by 2. Quotient is 3, remainder is 0. Note the remainder.
Divide 3 by 2. Quotient is 1, remainder is 1. Note the remainder.
Divide 1 by 2. Quotient is 0, remainder is 1. Note the remainder.

Reading the remainders from bottom to top, the binary representation of 27 is 11011.

2.Octal:
Divide 27 by 8. Quotient is 3, remainder is 3. Note the remainder.
Divide 3 by 8. Quotient is 0, remainder is 3. Note the remainder.

Reading the remainders from bottom to top, the octal representation of 27 is 33.

Hexadecimal:
Divide 27 by 16. Quotient is 1, remainder is 11 (B in hexadecimal). Note the remainder.

Reading the remainders, the hexadecimal representation of 27 is 1B.

So, in summary:
Binary: 27 in decimal is 11011 in binary.
Octal: 27 in decimal is 33 in octal.
Hexadecimal: 27 in decimal is 1B in hexadecimal.
Program
def myfun():
    dec_num=int(input("Enter a decimal number:"))
    print("The decimal value of ",dec_num," is: ")
    print(bin(dec_num)," in binary.")
    print(oct(dec_num)," in octal.")
    print(hex(dec_num)," in hexadecimal.")
        
if __name__=="__main__":
    myfun()


Output:

Enter a decimal number:28
The decimal value of 28 is:
0b11100 in binary.
0o34 in octal.
0x1c in hexadecimal.



More Questions


76 . Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal.
77 . Write a Python Program To Find ASCII value of a character.
78 . Write a Python Program to Make a Simple Calculator with 4 basic mathematical operations.
79 . Write a Python Program to Display Fibonacci Sequence Using Recursion.
80 . Write a Python Program to Find Factorial of Number Using Recursion.
81 . Write a Python Program to calculate your Body Mass Index.
82 . Write a Python Program to calculate the natural logarithm of any number.
83 . Write a Python Program for cube sum of first n natural numbers?
84 . Write a Python Program to find sum of array.



For latest job updates join Telegram Channel: https://t.me/sateeshm