Answer to Question #329191 in C++ for Huzaifa

Question #329191

Write a C++ program that converts a decimal number to a binary, octal, and

hexadecimal equivalents. First, the program will ask to fix a range for the lower and

upper limit in which the conversion is required. The lower limit should not be less than

0 and greater than the upper limit. For example, if you enter a negative number or

greater than the upper limit, then the program should print a message for the invalid

input and will ask you again to enter a decimal number within the range. If you

correctly specify the upper and lower limits, then the program will print a table of the

binary, octal and hexadecimal equivalents of the decimal numbers in the range of

lower limit through upper limit.

Specific grading criteria: In this program you should use all the loops (e.g., while,

do-while and for loop) each of them only one time. The output mentioned below is just

an example; more clear and well- presented output will improve your points.




1
Expert's answer
2022-04-16T03:46:20-0400
#include <iostream>
#include <string>


const std::string digits = "0123456789abcdef";


std::string convert(int number, int base) {
  std::string result = "";
  do {
    result = digits[number % base] + result;
    number /= base;
  } while (number > 0);
  return result;
}


int main() {
  int lower, upper;
  std::cout << "Please enter limits separated with space:\n";
  std::cin >> lower >> upper;
  while (lower < 0 || lower > upper) {
    std::cout << "The lower limit must not be less than 0 or greater than the upper limit\nPlease enter valid limits:\n";
    std::cin >> lower >> upper;
  }
  std::cout << "\n----------------------------------------------------------------\n";
  std::cout << "|              binary              |    octal    | hexadecimal |\n----------------------------------------------------------------\n";
  for (int i = lower; i <= upper; ++i) {
    std::string binary = convert(i, 2);
    std::string octal = convert(i, 8);
    std::string hexadecimal = convert(i, 16);
    std::string binary_space((34 - binary.size()) / 2, ' ');
    std::string binary_space2(34 - binary.size() - binary_space.size(), ' ');
    std::string octal_space((13 - octal.size()) / 2, ' ');
    std::string octal_space2(13 - octal.size() - octal_space.size(), ' ');
    std::string hexadecimal_space((13 - hexadecimal.size()) / 2, ' ');
    std::string hexadecimal_space2(13 - hexadecimal.size() - hexadecimal_space.size(), ' ');
    std::cout << '|' << binary_space << binary << binary_space2 << '|'
      << octal_space << octal << octal_space2 << '|'
      << hexadecimal_space << hexadecimal << hexadecimal_space2 << "|\n";
  }
  std::cout << "----------------------------------------------------------------\n\n";
  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