Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views2 pages

Problem2 DescriptionAndOutput

Uploaded by

pkm69782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Problem2 DescriptionAndOutput

Uploaded by

pkm69782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

DESCRIPTION OF PROBLEM 2

The provided Java program is a minor project that accomplishes the conversion of a decimal number to
its representation in any given target base.

Here's a breakdown of the key components of the program:

User Input:
 The program begins by using the Scanner class to interact with the user, prompting them to
enter a decimal number and a target base.
 It validates the user input to ensure that the decimal number is non-negative and the target
base is greater than 1.
Output:
 After obtaining user input and performing the conversion, the program outputs the original
decimal number and its representation in the specified target base.
Error Handling:
 The program incorporates error handling to address invalid user input.
 If the input does not meet the specified criteria, an error message is displayed, and the program
terminates early.
Conversion Method:
 The core functionality resides in the convertToAnyBase method, which takes a decimal number
(n) and a target base (b) as parameters.
 The method starts with a check for the base case. If the input n is zero, it immediately returns
the string "0" since zero in any base is always "0".
 The method initializes an empty string called 'result' to store the converted value.
 It then enters a while loop that continues until the decimal number n becomes zero.
 The remainder of the division of n by the target base b is calculated and stored in the variable
remainder.
 A character digit is determined based on whether the remainder is less than 10. If it is, '0' is
added to the remainder; otherwise, 'A' is added to the remainder minus 10. This is done to
handle bases greater than 10, where digits are represented by letters A, B, C, etc.
 The digit is then prepended to the front of the result string.
 The value of n is updated by performing integer division by b, effectively removing the last digit.
 Once the loop completes, the method returns the final result string, which now represents the
decimal number n in the specified base.
In summary, this method effectively converts a decimal number to its representation in any given base
by repeatedly calculating remainders and building the resulting string from the least significant digit to
the most significant digit. The logic is designed to handle bases up to 36, where digits are represented by
both numbers and letters.

CONCLUSION
In summary, this program provides a user-friendly interface for converting decimal numbers to any base,
incorporating input validation, conversion logic, error handling, and proper resource management.
OUTPUT 1:
Enter a decimal number: 123
Enter the target base: 16
Decimal number: 123
Converted to base 16: 7B

OUTPUT 2
Enter a decimal number: 123
Enter the target base: 12
Decimal number: 123
Converted to base 12: A3

OUTPUT 3
Enter a decimal number: -123
Enter the target base: 16
Invalid input. Decimal number should be non-negative, and target base should
be greater than 1.

OUTPUT 4
Enter a decimal number: 123
Enter the target base: 0
Invalid input. Decimal number should be non-negative, and target base should
be greater than 1.

You might also like