Answer to Question #272928 in C++ for Hamda

Question #272928

Write a c++ program using loops that outputs the binary, octal, and hexadecimal equivalent of the integer in the range from 1 to 256.

1
Expert's answer
2021-11-29T00:00:29-0500
// The oct, hex, and dec identifiers are stream manipulators
// oct causes integers to be output in octal, the manipulator
// hex causes integers to be output in hexadecimal, and the manipulator
// dec causes integers to be output in decimal.


#include <iostream>
using namespace std;
int main()
{
cout << "Decimal\t\tBinary\t\tOctal\tHexadecimal\n";
for ( int loop = 1; loop <= 256; ++loop )
{
cout << dec << loop << "\t\t";
// Output binary number
int number = loop;
cout << ( number == 256 ? '1' : '0' );
int factor = 256;
do
{
cout <<( number < factor && number >= ( factor / 2 ) ? '1' : '0' );
factor /= 2;
number %= factor;
}
while ( factor > 2 );
// Output octal and hexadecimal numbers
cout << '\t' << oct << loop << '\t' << hex << loop << endl;
}
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS