Basically Speaking
InterviewTime limit1sMemory limit128 MB
Convert a number from one base to another (bases 2 to 16) and print the result right-justified in a 7-character field, or ERROR if it exceeds 7 digits.
- Level
Easy3 of 10
- Topics
- Math, Implementation, String, Simulation
- Solved
- No attempts yet
Problem
You are building the base-conversion feature of a pocket calculator. The calculator has the following properties:
- It has a 7-digit display.
- Its buttons include the digits
0-9and the capital lettersA-F. - It supports bases 2 through 16.
Write a program that converts a number from one base to another and prints how the result would appear on the calculator's display.
Input
Each line of input describes one conversion and contains three values separated by one or more spaces:
- the number to convert, written in the source base;
- the source base (an integer from 2 to 16);
- the target base (an integer from 2 to 16).
Extra spaces may appear before, between, or after the values. Keep reading and processing conversions until the end of the input.
Output
For each conversion, print the converted number exactly as it would appear on the 7-digit display: right-justified within a field of width 7 (padded with leading spaces). Digits greater than 9 are shown as the capital letters A-F.
If the converted number needs more than 7 digits it cannot fit on the display; in that case print ERROR instead, also right-justified within the width-7 field.