Converting number to binary (zero and one)
Write a function that aThe number to binaryOr oneBinary string to numberTurn
Write a function that aThe number to binaryOr oneBinary string to numberTurn
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
let msg = 'error' alert(msg)This answer is only visible to premium members
This answer is only visible to premium members
def f(n): if n==0: return '0' m='' while n>0: m+=str(n%2) n=n//2 return m n=int(input(":")) print(f(n ))
x = int(input("Enter integer number: "))
lst = []
while x>0:
c = x%2
x = x//2
lst.append(c)
st = ""
for i in lst[::-1]:
st += str(i)
print(f"binery number is {st}")
number=int(input("enter a number for I show your number in binary number :) >>>>"))
binary_number=bin(number)
print("binaried your number--->",binary_number)
def convert_number_binary(value):
if isinstance(value, int):
return bin(value)[2:]
elif isinstance(value, str):
return int(value, 2)
else:
raise ValueError("Input must be an integer or a binary string.")
#include <iostream> #include <cmath> using namespace std; int q(long int x , string y){ long int b ,a; int i ,c = 1 ,n ,s ,d = 1 ,e = 1 ,j; b = 0; s = 0; a = x; for (n = 1 ; a / 10 > 0 ; n++){ a = a / 10; } if (y == "b"){ for (x ; x > 0 ; x = x / 2){ b = b + (x % 2) * c; c = c * 10; } return b; } else if (y == "o"){ for (i = 0 ; i < n ; i++){ s = s + ((x / d) % 10) * e; d = 10; e = 2; for (j = 0 ; j < i ; j++){ d = d * 10; e = e * 2; } } return s; } else{ cout<<"eror!!! : we have only binery and number !!!"; cin>>x; cin>>y; q(x , y); } } int main(){ long int x; string y; cout<<"enter the number : "; cin>>x; cout<<"would you like it to be binary {b} or Ordinary number {o} : "; cin>>y; cout<<q(x ,y); return 0; }
def q(x ,y ,n): b = 0 s = 0 c = 1 d = 1 e = 1 if y == "b": while x > 0 : b = b + (x % 2) * c c = c * 10 x = x // 2 return b elif y == "o": for i in range(n): s = s + ((x // d) % 10) * e d = 10 e = 2 for j in range(i): d = d * 10 e = e * 2 return s else: return "eror!!! : we have only binery and number !!!" X = input("enter the number : ") y = input("would you like it to be binary [b] or Ordinary number [o] : ") n = len(X) x = int(X) print(q(x ,y ,n))
def num_to_binary(num): binery = bin(num)[2:] return binery def binary_to_num(num): number = int(num,2) return number menu = int(input('1-num to binary 2-binary to num')) if menu == 1 : num = int(input('enter number: ')) print(num_to_binary(num)) elif menu == 2 : num = input('enter binary: ') print(binary_to_num(num))
Submitting answers is currently unavailable.
You must be logged in to access this section.
Login/Sign up If you don’t understand the exercise or can’t solve it for any reason, that’s completely
normal—don’t worry 😊
Try checking out easier exercises and reviewing different answers
submitted by others. Gradually, you can move on to more challenging exercises. Also, your answer
might be correct even if it’s different from others.