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
#include <iostream> #include <cmath> using namespace std; int main(){ int x; cout << "Enter a number"; cin >> x; cout << x << endl; int baghimande = 0; int bainery = 0; for(int i = 0; x>0; i++){ int a; baghimande = (x/2); a = x%2; bainery += (a*pow(10,i)); x = baghimande; } cout << bainery; }
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 ))
#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.
برای استفاده از این بخش باید وارد حساب کاربریت بشی
ورود/ثبت نام 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.