تبدیل عدد به باینری (صفر و یک)
تابعی بنویسید که یک عدد را به باینری یا یک رشته باینری را به عدد تبدیل کند
تابعی بنویسید که یک عدد را به باینری یا یک رشته باینری را به عدد تبدیل کند
#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;
}
n=int(input("Enter your number:"))
bi=''
while n>0:
bi=str(n%2)+bi
n=n//2
print('binary:',bi)
def binary():
output = 0
status = input('''
1. convert int to bin
2. convert bin to int
please choice:
''')
if status == '1':
inputing = int(input('enter the value:'))
output = bin(inputing)[2:]
elif status == '2':
inputing = input('enter the bin num:')
output = int(inputing, base=2)
else:
output = 'not currect. please try again'
print(output)
return binary()
print(output)
binary()
#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;
}
Py
def bin(n) :
binary = ""
while n > 1 :
if n == 3 :
binary += "11"
elif n == 2 :
binary += "01"
else :
binary += str(n%2)
n = n//2
print(binary[::-1])
while True :
n = int(input("Enter a number = "))
if n==0 :
print(0)
elif n==1 :
print(1)
else :
bin(n)
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)
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}")
import time
start_time = time.time()
class Binary:
def __init__(self,number):
self.number = number
def click_number(self):
bins = bin(self.number)
return bins
while True:
num = int(input("enter your number ="))
print(Binary(num).click_number())
if num == 0:
print("end while")
break
number=int(input("enter a number for I show your number in binary number :) >>>>"))
binary_number=bin(number)
print("binaried your number--->",binary_number)
جوابت را با Markdown بنویس و ثبت کن
برای استفاده از این بخش باید وارد حساب کاربریت بشی
ورود/ثبت نامتوضیح کوتاهی بنویسید تا تیم بررسی کند.