How do i write a program that allows the user to enter the base of a numbering system (2 for binary, 10 for decimal, 8 for octal..) Asks for four digits, and Based on the selection of the user, the code should convert the 4 digits to their decimal equivalent. This what i have so far...Im trying to figure out what formula im supposed to be using for the conversion or if im even in the right track
1. Please use [code]//Code goes in here. [/code] tags. It makes everybody's life easier.
2. What you call "Convert to decimal" is not a conversion at all. You are just asking the end user to type a number. There is no conversion there.
3. You calculation makes no sense. If you want to obtain the representation of a number in a specified base, you need to construct a string. The string's characters will be the digits in the specified base.
4. Your Display section congratulates the end user for answering a question correctly. What question? There is no question.
Sooo, restatrt. Think about it for a while before writing code, and then write code. Your objectives (I think):
1. Obtain a number from the end user.
2. Ask the end user what base they want to convert the number to.
3. Convert the given number to the specified base.
4. If this is a test, ask the user for the answer. Does it match your calculated digits? Congratulate the user, or smack him/her in the face for missing the right answer!
5. If it is not a test, just display the calculated digits.
Want to know how to calculate the individual digits? I bet Google will give you the formula quite easily.
Want to know how to calculate the individual digits? I bet Google will give you the formula quite easily.
I highly doubt there is a generic formula for converting to different bases, especially when you get past base 10 and start using letters. You really just need to think about how bases work, and how you can analyze a number. If you're, for example converting something to base 2 (binary), then continue generating powers of two that will be large enough to hold your number, and then work your way through them. Go through each place (starting from the left and moving forward), if that place value fits into the number then make it a 1, otherwise a zero. For example if we're converting 18 to binary you would do the following:
1 2 3 4 5 6 7 8 9 10 11 12
Generate all the powers of two we need:
2^0 = 1
2^1 = 2
2^3 = 4
2^4 = 8
2^5 = 16
Now start every bit at zero:
00000
Now start with the leftmost zero. How many 16s fit into 18? One, so change that bit to a 1. Now our number is:
10000
Continue this process with each digit, making it a one if it goes into the number. When you finish, you should have:
10010
It gets trickier in say, base 3 because you can have multiple amounts of each place. Hope this will give you a start :)
There is one simple, recursive formula that will convert to any base, 2 and up. No limit as long as you can find enough symbols for the digits. Usually you can go up to base 36, which is all digits, plus all of the English alphabet.