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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT