Write a program to find the decimal number from given Binary number


This program is a Python script that converts a decimal number to a binary number. The program takes input from the user in the form of a decimal number, and then uses a while loop to convert it to binary. The program uses the following steps to convert the decimal number to binary:

  • It takes the decimal number as input from the user and assigns it to the variable "decimal".
  • It initializes a variable "binary" to 0 and a variable "ctr" to 0. The variable "binary" will be used to store the binary equivalent of the decimal number and "ctr" will be used to keep track of the position of the current digit.
  • The program then enters a while loop that continues until the value of "temp" (which is initially set to the value of "decimal") becomes 0.
  • Inside the while loop, the program uses the modulus operator to find the remainder when "temp" is divided by 2. This remainder represents the next digit of the binary number. The program then multiplies this remainder by 10 raised to the power of "ctr" (to place the digit in the correct position) and adds the result to the "binary" variable.
  • The program then updates the value of "temp" to be the integer division of "temp" by 2, and increments the value of "ctr" by 1.
  • After the while loop ends, the program prints the decimal number and the binary number.

Source Code

"""
def convertToBinary(n):
   if n > 1:
       convertToBinary(n//2)
   print(n % 2,end ="")
 
decimal = int(input("Enter the Decimal Number :"))
convertToBinary(decimal)
 
 
"""
decimal = int(input("Enter the Decimal Number :"))
binary = 0
ctr = 0
temp = decimal
while(temp > 0):
    binary = ((temp%2)*(10**ctr)) + binary
    temp = int(temp/2)
    ctr += 1
print("Decimal Number :",decimal)
print("Binary Number :",binary)

Output

Enter the Decimal Number :23
Decimal Number : 23
Binary Number : 10111


Example Programs