Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. AnytoBase10:
  2.  
  3. I'm a bit tired so I'll keep this brief, you guys will have to work a little harder for this one.
  4.  
  5. Let's say your input is base 2: 1110
  6.  
  7. In order to convert that base 2 into decimal, you do each digit * 2^(i) + the next
  8.  
  9. that translates to: 0 *2^(0) + 1 * 2^(1) + 1 * 2(2) + 1 *2^(3)
  10. solve that, it equals 14.
  11.  
  12. if say, you wanted base 4 then again, input but times 4^(i) this time.
  13.  
  14. 2 * 4^(0) + 3 * 4(1) = 14
  15.  
  16. The trickiest part here is getting each digit seperated so I'll leave you with this
  17. ///
  18. number = int(input("Enter your number: "))
  19.  
  20. digit1 = number % 10
  21. digit2 = (number / 10) % 10
  22. digit3 = (number / 100) % 10
  23. digit4 = (number / 1000) % 10
  24.  
  25. print(int(digit1), int(digit2), int(digit3), int(digit4))
  26. /// run and print, input = 2196
  27. terminal: 6 9 1 2
  28.  
  29. now slap that into a for loop since you dont know how many times you'll have to go through this step.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement