In the previous article, we have discussed Python Program to Convert Decimal to Octal using While loop
Given a decimal number and the task is to get the respective binary number for a given number.
Examples:
Example1:
Input:
Given Number = 7
Output:
The Binary value of the given decimal number { 7 } is: 111
Example2:
Input:
Given Number = 9
Output:
The Binary value of the given decimal number { 9 } is: 1001
Program to Convert a Decimal Number to Binary in Python
Below are the ways to convert the given decimal number into Binary :
Method #1: Using While Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Take a variable say ‘a ‘and initialize its value with 1.
- Take another variable say binry_num and initialize its value with 0.
- Loop till the given number is not equal to zero using the while loop.
- Inside the loop, calculate the value of the given number modulus 2 and store it in a variable remdr.
- Divide the given number by 2 and store it in the same variable gvn_numb.
- Multiply a with the remdr and store it in another variable b
- Add b to the binry_num and convert to int using the int() function.
- Store it in the same variable binry_num.
- Multiply a with 10 and Store it in the same variable ‘a’.
- Print the binry_num to get the binary value of the given decimal number.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 7 # Take a variable say 'a 'and initialize its value with 1. a = 1 # Take another variable say binry_num and initialize its value with 0. binry_num = 0 print("The Binary value of the given decimal number {", gvn_numb, "} is: ") # Loop till the given number is not equal to zero using the while loop. while (gvn_numb != 0): # Inside the loop, calculate the value of the given number modulus 2 and # store it in a variable remdr. remdr = gvn_numb % 2 # Divide the given number by 2 and store it in the same variable gvn_numb. gvn_numb = gvn_numb // 2 # Multiply a with the remdr and store it in another variable b b = (remdr * a) # Add b to the binry_num and convert to int using the int() function. # Store it in the same variable binry_num. binry_num = int(binry_num + b) # Multiply a with 10 and Store it in the same variable 'a'. a = int(a * 10) # Print the binry_num to get the binary value of the given decimal number. print(binry_num)
Output:
The Binary value of the given decimal number { 7 } is: 111
Method #2: Using While loop (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Take a variable say ‘a ‘and initialize its value with 1.
- Take another variable say binry_num and initialize its value with 0.
- Loop till the given number is not equal to zero using the while loop.
- Inside the loop, calculate the value of the given number modulus 2 and store it in a variable remdr.
- Divide the given number by 2 and store it in the same variable gvn_numb.
- Multiply a with the remdr and store it in another variable b
- Add b to the binry_num and convert to int using the int() function.
- Store it in the same variable binry_num.
- Multiply a with 10 and Store it in the same variable ‘a’.
- Print the binry_num to get the binary value of the given decimal number.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. gvn_numb = int(input("Enter some random Number = ")) # Take a variable say 'a 'and initialize its value with 1. a = 1 # Take another variable say binry_num and initialize its value with 0. binry_num = 0 print("The Binary value of the given decimal number {", gvn_numb, "} is: ") # Loop till the given number is not equal to zero using the while loop. while (gvn_numb != 0): # Inside the loop, calculate the value of the given number modulus 2 and # store it in a variable remdr. remdr = gvn_numb % 2 # Divide the given number by 2 and store it in the same variable gvn_numb. gvn_numb = gvn_numb // 2 # Multiply a with the remdr and store it in another variable b b = (remdr * a) # Add b to the binry_num and convert to int using the int() function. # Store it in the same variable binry_num. binry_num = int(binry_num + b) # Multiply a with 10 and Store it in the same variable 'a'. a = int(a * 10) # Print the binry_num to get the binary value of the given decimal number. print(binry_num)
Output:
Enter some random Number = 9 The Binary value of the given decimal number { 9 } is: 1001
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.