Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81af0e9

Browse files
added base conversion
1 parent 2ac0c8a commit 81af0e9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎Day 1/base-convert.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def convert_from_10(x, base):
2+
s = ''
3+
while x > 0:
4+
rem = x % base
5+
rem = str(rem) if rem < 10 else chr(65 + rem - 10)
6+
s = rem + s
7+
x //= base
8+
return s
9+
10+
11+
n = input('Enter a number: ')
12+
base = int(input('Enter its base: '))
13+
to_base = int(input('Enter the base you want to convert it to: '))
14+
15+
x = int(n, base)
16+
y = convert_from_10(x, to_base)
17+
18+
print(y)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /