Entri Blog
No Result
View All Result
Sunday, February 5, 2023
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
FREE GK TEST: SIGNUP NOW
Entri Blog
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET
No Result
View All Result
Entri Blog
Free GK Test
banner top article banner top article
Home Articles

Python Program to Convert Decimal to Binary Number

by Akhil M G
December 6, 2022
in Articles, Data Science and Machine Learning, Java Programming, React Native, Web and Android Development
Python Program to Convert Decimal to Binary Number
Share on FacebookShare on WhatsAppShare on Telegram

In this article, we’ll look at some programs that translate decimal numbers into their binary equivalents. Two Python programmes will be shown; the first program performs the conversion using a user-defined function, and the second program uses the built-in function bin() for the conversion of decimal to binary. The assignment is to create a Python program that will translate a given decimal number into its equivalent binary number.

Examples :

Input : 7                                                         
Output :111

Input :10
Output :1010

Method #1: Recursive solution

DecimalToBinary(num):
        if num >= 1:
            DecimalToBinary(num // 2)
           print num % 2

Below is the implementation of the above recursive solution:

Get python programming course with Entri app
  • Python3
# Function to convert decimal number
# to binary using recursion
def DecimalToBinary(num):
    
    if num >= 1:
        DecimalToBinary(num // 2)
    print(num % 2, end = '')
# Driver Code
if __name__ == '__main__':
    
    # decimal value
    dec_val = 24
    
    # Calling function
    DecimalToBinary(dec_val)

Output

011000

Method #2: Decimal to binary using in-built function

  • Python3

# Python program to convert decimal to binary
  
# Function to convert Decimal number
# to Binary number
def decimalToBinary(n):
    return bin(n).replace("0b", "")
  
# Driver code
if __name__ == '__main__':
    print(decimalToBinary(8))
    print(decimalToBinary(18))
    print(decimalToBinary(7))

Output

1000
10010
111

Method #3:Without in-built function

  • Python3

# Python program to convert decimal to binary
  
# Function to convert Decimal number
# to Binary number
def decimalToBinary(n):
    return "{0:b}".format(int(n))
  
# Driver code
if __name__ == '__main__':
    print(decimalToBinary(8))
    print(decimalToBinary(18))
    print(decimalToBinary(7))

Output

1000
10010
111

Using the bitwise shift operator >>.

  • Python3

def dec2bin(number: int):
    ans = ""
    if ( number == 0 ):
        return 0
    while ( number ):
        ans += str(number&1)
        number = number >> 1
    
    ans = ans[::-1]
    return ans
def main():
    number = 60
    print(f"The binary of the number {number} is {dec2bin(number)}")
# driver code
if __name__ == "__main__":
    main()

Output

The binary of the number 60 is 111100
Share62SendShare
Akhil M G

Akhil M G

Related Posts

Articles

Top CSS Tools for Web Developer in 2023

February 3, 2023
TNPSC Inspector of Fisheries Admit Card 2023 Out
Admit Card

TNPSC Inspector of Fisheries Admit Card 2023 Out: Download Here

February 3, 2023
Data Science Jobs in Kerala
Articles

Data Science Jobs in Kerala

February 3, 2023
Next Post
Difference Between Retesting and Regression Testing

Difference Between Retesting and Regression Testing

Discussion about this post

Latest Posts

  • Learn Continuous Tense Examples
  • Examples of Adverb in English Sentences
  • Slogans on World Environment Day
  • Top CSS Tools for Web Developer in 2023
  • TNPSC Inspector of Fisheries Admit Card 2023 Out: Download Here

Trending Posts

  • states of india and their capitals and languages

    List of 28 States of India and their Capitals and Languages 2023 – PDF Download

    149828 shares
    Share 59928 Tweet 37455
  • List of Government Banks in India 2023: All you need to know

    61097 shares
    Share 24439 Tweet 15274
  • TNPSC Group 2 Posts and Salary Details 2022

    39459 shares
    Share 15784 Tweet 9865
  • New Map of India with States and Capitals 2023

    28565 shares
    Share 11426 Tweet 7141
  • Odisha Police Recruitment 2023 PDF Download for 4790 Posts – Eligibility, Selection Process

    863 shares
    Share 345 Tweet 216

Company

  • Become a teacher
  • Login to Entri Web

Quick Links

  • Articles
  • Videos
  • Entri Daily Quiz Practice
  • Current Affairs & GK
  • News Capsule – eBook
  • Preparation Tips
  • Kerala PSC Gold
  • Entri Skilling

Popular Exam

  • IBPS Exam
  • SBI Exam
  • Railway RRB Exam
  • Kerala PSC
  • Tamil Nadu PSC
  • Telangana PSC
  • Andhra Pradesh PSC
  • MPPSC
  • UPPSC
  • Karnataka PSC
  • Staff Selection Commission Exam

© 2021 Entri.app - Privacy Policy | Terms of Service

No Result
View All Result
  • State Level PSC
    • Kerala PSC
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
  • SSC
  • Railway
  • Entri Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • TET
    • APTET
    • CTET
    • DSSSB
    • Karnataka TET
    • Kerala TET
    • KVS
    • MPTET
    • SUPER TET
    • TNTET
    • TSTET
    • UPTET

© 2021 Entri.app - Privacy Policy | Terms of Service