CodeSolved

Solved Programming Questions & Exercises

Converting number to binary (zero and one)

Practice Easy 1523/ Download 1385 Views

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

11 Answers

This answer is only visible to premium members
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

This answer is only visible to premium members

Subscription is currently unavailable.
This answer is only visible to premium members
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

This answer is only visible to premium members

Subscription is currently unavailable.
This answer is only visible to premium members
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

This answer is only visible to premium members

Subscription is currently unavailable.
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
number=int(input("enter a number for I show your number in binary number  :) >>>>"))
binary_number=bin(number)
print("binaried your number--->",binary_number)
Shina Download Python
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.")
Ai 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))
<< Previous page 1 2 Next page >>

Submit answer

Submitting answers is currently unavailable.

×
×
Close