Question

Could you please help me with an answer to this question in a simple way for the beginner? This is a beginner level class so can you please help me. The code must have beginner level implementation. Can you also please explain what things do. Thank you!

Write a program that prompts the user for a positive integer number smaller than 256 (i.e. in [0...255]) and converts it into

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

Code:  Below mentioned is C++ code. Similar code can be written in C/Java as only input/output functions would change.

#include<iostream>
using namespace std;
main(){
int num,length,n;
cout<<"Please enter a number between 0 and 255: ";
cin>> num; // Taking input
n=num; //Copy of input number for operations so that input number is stored intact
if(num>255 || num<0){ // Validating range
cout<<"Number out of range";
exit(0);
}
string binary=""; // Initializing Null (empty) String
while(n>0){ // Process of converting decimal to binary
binary=to_string(n%2)+binary; // Concatenating Strings (each new bit in front of other bits)
n=n/2;
}
length=binary.length(); // If length of binary is less than 8 then convert add trailing zeros in front
if(length<8){
for(int i=length+1;i<=8;i++)
binary="0"+binary;
}

cout<<"The binary equivalent of "<<num<<" is "<<binary;
}

Explanation: Program takes an integer as input and checks if it lies in range of 0-255. If it does not then it terminates program. If number is in specified range then program initializes an empty string and start adding (concatenating) binary representation of number bit by bit. if length of binary representation is less than 8 bits then it is padded by trailing zeroes.

Output:

Code:

#include<iostream> using namespace std; main() { int num, length, n; cout<<Please enter a number between 0 and 255: ; cin>>

Please enter a number between 0 and 255: 83 The binary equivalent of 83 is 01010011 Process returned © (@xo) execution time :

Add a comment
Know the answer?
Add Answer to:
Could you please help me with an answer to this question in a simple way for...
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 help me to write a program to check telephone number validity . (just check all...

    Please help me to write a program to check telephone number validity . (just check all characters are "0"~"9") 1.C language 2.Please use "printf"(no scanf、gets....) 3.only argv ,argc accepted 4.Please explain the steps (cause I'm a beginner hhhhh) Thank you soooooo much!!!!!

  • i have received an irrelevant answer, please answer if you do want help. Thanks URGENT could...

    i have received an irrelevant answer, please answer if you do want help. Thanks URGENT could you please help me go through the solution of (d and e) expecting a detailed answer Thanks (c) Does adding 2t to an n-bit number affect any of the least significant n bits of the resulting (n+1)-bit number? (d) Use the above result and the fact that a (b-c) (a c)+b to explain how the two's complement representation of a negative number may be...

  • Hi Guys, Just wondering if someone could help me with the below, specifically detailed so that...

    Hi Guys, Just wondering if someone could help me with the below, specifically detailed so that I can understand in the future. Thanks in advance! he Factor Finder takes a 4 bit binary number entered by the user (representing the values 0 to 15) and tells the user if the number is zero, odd, even or a multiple of 3, 4, or 5 The input and output details are as follows: Digital Inputs: I1: Bit O of the four digit...

  • Could you please help me to solve the problem. Also, could you please answer questions in...

    Could you please help me to solve the problem. Also, could you please answer questions in clear hand-writing and show me the full process, thank you (Sometimes I get the answer which was difficult to read).Thanks a lot Suppose we are comparing implementations of two algorithms on the same machine. For input size of n, Algorithm A runs in 8n^2 steps, while Algorithm B runs in 64nlog2(n) steps. For what value n>2, where n is an integer, does Algorithm A...

  • Could you please help me with this assignment? The purpose of this assignment is to simulate...

    Could you please help me with this assignment? The purpose of this assignment is to simulate the transmission and reception of a text message (i.e., character string) over a noisy communications channel. Suppose we wish to transmit the string “Hello.” We can translate this string into a bit stream as shown below: String: H e l l o . \0 (note: the last character \0 is the null terminator character) ASCII: 72 101 108 108 111 46 0 Binary: 01001000...

  • write codes and give detailed explanation of how to execute. What you need to turn in:...

    write codes and give detailed explanation of how to execute. What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word or PDF...

  • IMPORTANT: I know the answer is "B". Could you please help explain to me the reasoning...

    IMPORTANT: I know the answer is "B". Could you please help explain to me the reasoning behind why that is? Thank you One implication of the fact that profit functions are convex in prices is that firms will always prefer a. stable input and output prices. b. input and output prices that fluctuate about a given level. c. stable input prices and fluctuating output prices. d. fluctuating input prices and stable output prices.

  • Could you please read 7483 data sheet and then answer number e 7383 Data Sheet 5483A...

    Could you please read 7483 data sheet and then answer number e 7383 Data Sheet 5483A 4-Bit Binary Full Adder with Fast Carry General Description The '83A high speed 4-bit binary full adders with internal carry lookahead accept two 4-bit binary words (Ao-A3, Bo- B3) and a Carry input (Co). They generate the binary Sum outputs (So-S3) and the Carry output (C4) from the most significant bit. They operate with either HIGH or active LOW operands (positive or negative logic)....

  • Please help me with this question! If you could explain your answer and relate it to...

    Please help me with this question! If you could explain your answer and relate it to the HNMR shown on the right that would be great. Thanks! Draw the major product that results from the reaction below. Grignard, then condensation. HNMR is on the right with integrations in red. I'm pretty sure they're right this time. (2) MgBr We were unable to transcribe this image

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

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