1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
vivado [14]
3 years ago
12

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
Computers and Technology
1 answer:
Maru [420]3 years ago
3 0

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

<em>    if num == 0: </em>

<em>        return 0 </em>

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)))

You might be interested in
This project involves writing a program that encodes and decodes messages. The program should prompt the user to select whether
Lilit [14]

Answer:

See explaination

Explanation:

import java.util.Scanner;

public class EncodeDecodeMessage {

public static String encode(String str) {

String result = "";

char ch;

for(int i = 0; i < str.length(); ++i) {

ch = str.charAt(i);

if(Character.isLowerCase(ch)) {

result += (char)('a' + (25-ch+'a'));

} else if(Character.isUpperCase(ch)) {

result += (char)('A' + (25-ch+'A'));

} else {

result += ch;

}

}

return result;

}

public static String decode(String str) {

return encode(str); // since the scheme is same, we can use encode for decode

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("1. Encode, 2. Decode. Enter your choice: ");

int choice = in.nextInt();

in.nextLine();

if(choice == 1) {

System.out.print("Enter sentence to encode: ");

String line = in.nextLine();

System.out.println("Encoded string is: " + encode(line));

} else if(choice == 2) {

System.out.print("Enter sentence to decode: ");

String line = in.nextLine();

System.out.println("Decoded string is: " + decode(line));

}

}

}

8 0
3 years ago
compare the two methods of creating table in ms access ( Datasheet view and Design view) which one wouldyou prefer to use to rec
Snezhnost [94]

The main difference between the datasheet view and the design view is in the datasheet view, we can only view and do minor changes but in the design view, we can edit the table and modify it.

<h3>What is MS Access?</h3>

MS Access is a service of Microsoft, that is used in the sector of business and other companies, to store and manage data.

Datasheet view and design view are the two types of tables in ms access, the design view has more functions, you can change and edit the table.

Thus, the primary distinction between the datasheet view and the design view is that while we can examine and make some minor adjustments in the datasheet view, we can edit and amend the table in the design view.

Learn more about MS Access, here:

brainly.com/question/17135884

#SPJ1

5 0
2 years ago
The correct syntax for accessing the length of an array named numbers is:
Sladkaya [172]
The syntax is truly dependent on the programming language, as different languages have different API's. 

But, the most standard form of accessing an array length is via a method or property by dot notation.

For example -

for(int i = 0; i <= m_Array.Length; i++) {...}

This code is from C#, using the Length property.
3 0
3 years ago
How do you change your name because i really would like to change but i am not sure how to could someone help with it please
belka [17]
Goto your profile settings

3 0
3 years ago
How many times does the following loop execute?int upperCaseLetters = 0;String str = "abcdEfghI";boolean found = false;for (int
damaskus [11]

Answer:

Five times

Explanation:

Given the codes as follows:

  1.        int upperCaseLetters = 0;
  2.        String str = "abcdEfghI";
  3.        boolean found = false;
  4.        for (int i = 0; i < str.length() && !found; i++)
  5.        {
  6.            char ch = str.charAt(i);
  7.            if (Character.isUpperCase(ch))
  8.            {
  9.                found = true;
  10.            }
  11.        }

The for loop will stop when the first uppercase letter is found in the str. This condition is set in the for loop condition ( i < str.length() && !found)

Since the the first upper case letter is the fifth character in the given str, the for loop will run for five rounds. In the fifth round the condition in the if statement (Line 9) will be evaluated to true and then set the true value to found variable (Line 11). This will terminate the loop in the next iteration.

7 0
3 years ago
Other questions:
  • Two users, UserA and UserB, are engaging in secure communication using only asymmetrical encryption. UserA needs to send a secur
    11·1 answer
  • PLEASE HELP I WILL METAL FOR AWNSER
    11·2 answers
  • An empty frame used to reserve space for a picture to be inserted at a later time is know as what
    6·1 answer
  • 5. Write a function that takes two lists of integers and returns a list containing tuples with corresponding elements from both
    8·1 answer
  • 1. What runs horizontally and is identified with numbers?
    12·2 answers
  • Write a java program to read elements in an array from user and count total number of duplicate elements in array.
    12·1 answer
  • Who is responsible for maintaining, monitoring, and analyzing database security at the back end?
    7·1 answer
  • Please do in python.Given an array of integers, create a 2-dimensional array where the first element is a distinct value from th
    13·1 answer
  • Your friends are having difficulties with their computer setups. Can you suggest a way to help each friend?
    5·1 answer
  • PLEASE HELP!! THIS IS DUE SOON.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!