CodeSolved

Solved Programming Questions & Exercises

Converting number to binary (zero and one)

Practice Easy 1523/ Download 1192 Views

Write a function that aThe number to binaryOr oneBinary string to numberTurn

9 Answers

n=int(input("Enter your number:"))
bi=''
while n>0:
    bi=str(n%2)+bi
    n=n//2
print('binary:',bi)

User 2785 Download Python
#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;
}
User 2720 Download C & C++
a=input("choose: 1.Convert a number in base ten to binary  2.Convert the number in binary form to base 10\n")
if a=="1":
    b=int(input("Enter a number in base ten : \n"))
    l=[]
    j=b
    while True:
        c=j%2
        d=j//2
        l.append(c)
        if d>=2:
            j=d
            continue
        else:
            l.append(d)
            break
    k=len(l)
    print("Number in binary form = ",end="")
    while k>0:
        print(l[k-1],end="")
        k-=1
if a=="2":
    r=input("Enter the number in binary form : \n")
    y=[]
    t=len(r)
    while t>0:
        y.append(r[t-1])
        t-=1
    m=0
    for i,j in enumerate(y):
       m+=int(j)*(2**i)
    print("Number in base 10 =",m)

Zaras Download Python
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}")
Najme.s.y Download Python
#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;
}
Aref.2 Download C & C++
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))
Aref.2 Download Python
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))
import pickle

data = int(input("Enter  your thing to change them to binary : "))
f = open("data.pkl","wb)
result = pickle.dump(data, f)
f.close()

print(result)

User 4925 Download Python

Submit answer

Submitting answers is currently unavailable.

×
×
Close