mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
Implement Three New Algorithms (#948)
* Create average_median.py I created a program to calculate the median of a list of numbers. * Changed Odd to Even in String * Create decimal_to_binary.py - Added 'conversions' folder. - Created a decimal to binary converter. * Implemented Decimal to Octal Algorithm - I created a decimal to octal converter based on the converter in the TheAlgorithms/Python project. - I added two newlines to make the output of decimal_to_binary.py better.
This commit is contained in:

committed by
Anup Kumar Panwar

parent
217615abf6
commit
1c9d995b9e
25
conversions/decimal_to_binary.py
Normal file
25
conversions/decimal_to_binary.py
Normal file
@ -0,0 +1,25 @@
|
||||
"""Convert a Decimal Number to a Binary Number."""
|
||||
|
||||
|
||||
def decimal_to_binary(num):
|
||||
"""Convert a Decimal Number to a Binary Number."""
|
||||
binary = []
|
||||
while num > 0:
|
||||
binary.insert(0, num % 2)
|
||||
num >>= 1
|
||||
return "".join(str(e) for e in binary)
|
||||
|
||||
|
||||
def main():
|
||||
"""Print binary equivelents of decimal numbers."""
|
||||
print("\n2 in binary is:")
|
||||
print(decimal_to_binary(2)) # = 10
|
||||
print("\n7 in binary is:")
|
||||
print(decimal_to_binary(7)) # = 111
|
||||
print("\n35 in binary is:")
|
||||
print(decimal_to_binary(35)) # = 100011
|
||||
print("\n")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user