Question

Write a program that will ask the user for a decimal number such as 18 and...

Write a program that will ask the user for a decimal number such as 18 and prints the 16 bit binary equivalent of the number: 0000 0000 0001 0010

NOTE: Your output should be a binary string formatted by nibbles (spaces every four bits): 0000 0000 0010 1110 1010 0111

1. Ask the user for a number and store it in an int variable called “number”.

2. Find out how many bits you want to use to represent this number. The project indicates 16 bits. Find the value of 2 to the power of this indicated number (2 to the power of 16-1). You may use the pow() function from the cmath library or use a loop to calculate this number (I prefer this method as it gives you a reason to practice your loops) Do not use the number for this vlaue as a constant number because this number will be changed next time the program compiles. So, we have this number and we’ll call it pow2.

3. Go into a loop (let's call this the subtraction loop) and do the following:

(1). subtract pow2 from the number you inputted. Put a flow chart here.

1. if the result is non-negative,

a. output a 1

b. replace the number with the result of the subtraction

2. if the result is negative

a. output a zero

(2). at the bottom of the subtract loop, divide the pow2 by 2

(3) continue this loop until pow2 drops to 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    int number;
    cin>>number;

    int bits=16;//number of bits used to represent the number

   long pow2 = pow(2,15);
   int result;

    int spaceCount =0;
   while(pow2>=1)//subtraction loop
   {


       if(spaceCount%4==0 && spaceCount>0)
        {
            cout<<" ";
            spaceCount=0;
        }

       result = number - pow2;
       if(result<0)//negative result
       {
           cout<<"0";
       }
       else//non-negative result
       {
           cout<<"1";
           number = result;
       }
       pow2/=2;
       spaceCount++;
   }


}


C\Users Mayank Bhardwaj\Documents\HackerEarthla.exe 18 0000 0000 0001 0010 Process returned (0x0 execution time 1.493 s Pre

Add a comment
Know the answer?
Add Answer to:
Write a program that will ask the user for a decimal number such as 18 and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please show work! 2. Now, give it a try by converting the binary number 01110110 to...

    Please show work! 2. Now, give it a try by converting the binary number 01110110 to decimal by filling in the same table in step 1 r of 2 Pov 128 64 32 16 Cumulative Amount 4. Now, you give it a try by converting the decimal number 131 to binary by filling in the table Power of 2 128 32 16 Bit Amount Remaining 6. Use the binary to hexadecimal table to convert the binary number 01101111 to hexadecimal...

  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

  • Guess the number! You will create a program that will ask the user to guess a...

    Guess the number! You will create a program that will ask the user to guess a number between 1 and 10. The pseudocode is below. Be sure to import random at the beginning of your code and use a comment block explaining what your program does #Guess the number week 4 #Name: #Date: #Random number, loop while true #ask user for number. #if number is too high or too low, tell user, if they guessed it break out of loop...

  • Ask uFor Ex. "Prompt user to enter a number" = user enters 10, your program should...

    Ask uFor Ex. "Prompt user to enter a number" = user enters 10, your program should output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.ser to input a number, and then using a loop control statement (While, For, Do-While), output to the console the numbers beginning from 1, up to and including the number input by the user.

  • Write a program and flowchart. The program should ask the user how many customers they want...

    Write a program and flowchart. The program should ask the user how many customers they want to track. Then, it asks for customer numbers, and sales by customer. At the end, it prints out the customer number, the customer sales, and then the total sales and average sales. You must use two different arrays, one for customer number, and one for sales. These are, in effect, parallel arrays.   You must use a "while" loop to gather customer number and sales....

  • I think you are actually doing binary to hex. please do this without loops and you...

    I think you are actually doing binary to hex. please do this without loops and you can use recursion. please write a working C code. Thanks Write a loop-less function to convert from Hex to Binary. (HINT: Use a helper function/recursion) binHex[16] [5] {"0000", "O001","0010","0011","0100" ,"0101", "0110", "0111", "1000", "1001" , "1010", "1011", "1100", "1101", "1110" 1 const char = s char hexToBinary ...) 7

  • Given the interpretation and the word(s), tell what characters or decimal numbers are stored in main memory by the desig...

    Given the interpretation and the word(s), tell what characters or decimal numbers are stored in main memory by the designated word(s). We assume our computer uses 8 bits for characters and 16 bits for binary integers: (show your work) Word I 1011 0010 0010 0000 Word II 0100 1001 0101 0010 1) Binary Integer - Word I 2) Character (ASCII) – Word II _____________________________________________________________________________________________________________________________________________________ For the following problems, assume that our computer uses 16 bits for binary integers. Find the...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • C++ Your goal is to ask the user for a number and to determine if that...

    C++ Your goal is to ask the user for a number and to determine if that number is a prime number. Use a function to determine if the number is prime. Your program should have the following:  The name of the program should be Assignment 6.  3 comment lines (description of the program, author, and date).  Ask the user for an integer. Store this result in a variable with an appropriate name. (1 point)  Write a...

  • This program will ask the user to enter a number of players, then ask the user...

    This program will ask the user to enter a number of players, then ask the user for each player's name and score. Once all of the players have been entered, the program will display a bar chart with each of their scores scaled as a percentage of the maximum score entered. See below for details on exactly how this should work - an example transcript of how the program should work is shown below (remember that values entered by the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT