Question

(Base13 to decimal) Hitchhiker’s Guide to the Galaxy proposed that the answer to the ultimate question...

(Base13 to decimal) Hitchhiker’s Guide to the Galaxy proposed that the answer to the ultimate question
was 42. It turned out that the corresponding question was ‘what is 9 times 6’. Some of you with a
modicum of Math skills might argue that is not right, but it is right if we are working with a base 13
system. This question requires you to input a 3-digit Base 13 number and convert it to its equivalent
decimal representation (note in Base 13 A is decimal 10, B is decimal 11, and C is decimal 12). Here are
some sample runs that your java program might produce:
<Output>
Enter a three-digit Base 13 string: 042
The decimal number for 042 is 54

i can not use array.

Homework Answers

Answer #1
import java.util.Scanner;

public class Base13Conversion {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a three-digit Base 13 string: ");
        String s = in.next();
        int d = 0;
        char ch;
        for (int i = 0; i < s.length(); i++) {
            d *= 13;
            ch = s.charAt(i);
            if (ch >= '0' && ch <= '9')
                d += ch - '0';
            else if (ch == 'a' || ch == 'A')
                d += 10;
            else if (ch == 'b' || ch == 'B')
                d += 11;
            else if (ch == 'c' || ch == 'C')
                d += 12;
        }
        System.out.println("The decimal number for " + s + " is " + d);
    }
}

Know the answer?
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
Similar Questions
Define a class BinaryNumber that represents binary numbers and a few simple operations on them, as...
Define a class BinaryNumber that represents binary numbers and a few simple operations on them, as indicated below. An example of a binary number is 1011 Its length is 4. Note that its leftmost digit is the least significant one: it represents the decimal number 1∗20 +0∗21 +1∗22 +1∗23 = 13. This is called little-endian format. You may use big-endian if you prefer; in that case you must state so at the beginning of your code as part of the...
If you cant answer this please dont waste my question. thank you. This cryptographic program run...
If you cant answer this please dont waste my question. thank you. This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key....
Read the attached articles about the proposed merger of Xerox and Fujifilm. Utilizing your knowledge of...
Read the attached articles about the proposed merger of Xerox and Fujifilm. Utilizing your knowledge of external and internal analysis, business and corporate strategy, and corporate governance, please discuss the following questions: 1. What is the corporate strategy behind the merger of Xerox and Fujifilm? 2. Why did Xerox agree to the merger? Is this a good deal for Xerox? Discuss the benefits and challenges they face with the merger. 3. Why did Fujifilm agree to the merger? Discuss the...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT