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
  • 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...

  • 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...

  • 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....

  • 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.

  • 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...

  • IN PERL Write a program that will ask the user for a number and will print...

    IN PERL Write a program that will ask the user for a number and will print all the integers in order starting from 1 until the number. Use the <> operator to ask for the user's input. For this program you will write a countdown timer. Your time will count down from 1 minute (60 seconds) to 0. Once the timer gets to 0 seconds the system will display a message that reads: RING RING RING! -using do while-

  • Question 4 (3 mark) : Write a Java program to ask the user to input an...

    Question 4 (3 mark) : Write a Java program to ask the user to input an integer number from the keyboard. The output should indicate whether it is positive, zero, or negative, an even number or an odd number. REQUIREMENTS • Your code should ask user to input an integer number, then decide its sign and parity based on this input. • Your code should use if-else statement. Your code must work exactly as the specification and the output should...

  • Create a program that will ask for the name, age, address, and favorite food of the...

    Create a program that will ask for the name, age, address, and favorite food of the user. After which, create a calculator with ADD, SUB, MULTI, and DIV. Then the program will display all the user have inputted in bash program ACTIVITY 4: Create a program that will ask for the name, age, address, and favorite food of the user. After which create a calculator with ADD, SUB, MULTI, and DIV. Then the program will display all the user have...

  • This is a c++ question we are not using namespace std at the top Subtraction +...

    This is a c++ question we are not using namespace std at the top Subtraction + Decision and Loop This is a twist on the previous exercise that will help you review loops and decision structures. You will again ask the user to enter two numbers. However, you will ALWAYS subtract the smaller number from the larger number to ensure that you never get a negative number for an answer. You do this by checking the numbers and switching them...

  • Please show that it works and show the output. Description Develop a program (in C++) that...

    Please show that it works and show the output. Description Develop a program (in C++) that first asks the user to input a signed number, for example 48, then converts it to binary number by using Two's Complement regulations. Do the same for the second inputted number, eg. 17. Then compute the summation of these two numbers, output the results in binary format first, then if there is an overflow, display error message; otherwise, convert the result to decimal value...

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
ADVERTISEMENT