Write a program that asks the user to enter a positive integer that represents a number in the decimal system and then displays the binary representation of the number. Your solution must include at least the following function: decimalToBinary(number): a recursive function that takes a positive integer number (in decimal) as its parameter and returns a string with it's binary representation. g

Respuesta :

Answer:

In Python:

def decimalToBinary(num):

   if num == 0:

       return 0

   else:

       return(num%2 + 10*decimalToBinary(int(num//2)))

       

decimal_number = int(input("Decimal Number: "))

print("Decimal: "+str(decimalToBinary(decimal_number)))

Explanation:

This defines the function

def decimalToBinary(num):

If num is 0, this returns 0

   if num == 0:

       return 0

If otherwise

   else:

num is divided by 2, the remainder is saved and the result is recursively passed to the function; this is done until the binary representation is gotten

       return(num%2 + 10*decimalToBinary(int(num//2)))

       

The main begins here.

This prompts the user for decimal number

decimal_number = int(input("Decimal Number: "))

This calls the function and prints the binary representation

print("Decimal: "+str(decimalToBinary(decimal_number)))

In this exercise we have to use the knowledge in computational language in python to describe a code that best suits, so we have:

The code can be found in the attached image.

To make it simpler we can write this code as:

def decimalToBinary(num):

  if num == 0:

      return 0

  else:

      return(num%2 + 10*decimalToBinary(int(num//2)))

decimal_number = int(input("Decimal Number: "))

print("Decimal: "+str(decimalToBinary(decimal_number)))

See more about python at brainly.com/question/19705654

Ver imagen lhmarianateixeira
ACCESS MORE