Check number is binary or not in python by R4R Team

Binary number

-It is a number which expressed in the base-2 numeral system,
-Binary numbers are 0 or 1.

Program to check the input number is binary or not-

n=int(input("Enter binary numbern"))

def check(n):
while(n):
if n%10==1 or n%10==0:
pass
else:
return 0
n=int(n/10)
return 1

if check(n):
print("Yes it is binary number")
else:
print("It is not binary number")


output -

Enter binary number
1001
Yes it is binary number

Enter binary number
121
It is not binary number


-In this program, we check the input number is binary or not i.e, bits are 0 or 1 only. so check() function is check the bits are 0,1 or not if not then it return 0 otherwise return 1.


Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!