Ticker

6/recent/ticker-posts

Program to find complement of a number


Program


 def fc(num):

    bina=bin(num)[2:]

    complement=''

    for b in bina:

        if b=='1':

            complement+='0'

        else:

            complement+='1'

            

    return  int(complement,2)

n=int(input("enter number"))

print(fc(n))


Output

enter number5

2

=== Code Execution Successful ===


In this program bin function is used to convert decimal number into binary and int function convert binary number to decimal.

Example

enter a number 5

First convert number 5 into binary  101

Then complement the number (1 convert into 0 and 0 convert into 1) so complement of binary number 5 is 010.

After find the complement then convert it into integer so 010 integer number is 2.

So complement of number 5 is 2.


Post a Comment

0 Comments