Duodecimal in Java



The duodecimal number system is also known as the base-12 number system. The decimal system (base-12) uses digits 0-9, while the duodecimal system uses digits 0-9 along with two additional symbols, A and B. In duodecimal, 10 represents 12 , which means 12 in the decimal number system is 10 in the duodecimal system. We often encounter duodecimal in daily life; for example, we have 12 months in a year, 12 zodiac signs, 12 inches in a foot, 12 musical notes in an octave, and we call a group of 12 items a dozen.

Representation of duodecimal numbers

The duodecimal system (base-12) represents the numbers using 12 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A and B. The symbols A and B represent the numbers 10 and 11, respectively. Other symbols like X and E pronounced as "dec" and "el" are also used instead of A and B. Each digit in a duodecimal number has a place value of power 12 instead of 10 in decimal. For example,

Let's take the duodecimal number 2A4.
 '2' represents 2 * (12^2) = 2 * 144 = 288
'A' represents 10 * (12^1) = 10 * 12 = 120
'3' represents 4 * (12^0) = 4 * 1 = 4
Adding these values together, we get 288 + 120 + 4 = 412 
Therefore, the duodecimal number 2A4 is equal to the decimal number 412.

Advantages of the base-12 number system

  • One major advantage is that 12 is divisible by 2, 3, 4, 6 and 12 while 10 is only divisible by 2, 5 and 10. So 12 can be divided into thirds, quarters, halves easily. 10 does not quarter easily. Thirds, quarters and halves are units we find easy to work with. This makes division and other mathematical operations easier in duodecimal number system than in the decimal number system.
  • There is evidence that many regions in Africa and Asia have used a base-12 counting system. We got 10 fingers and toes, which makes it easier to count in decimal. But we do have 12 phalanges (finger bones). In some regions of Asia, people use this method to count up to 144 on their fingers.
  • Famous mathematician A.C. Aitken, who was a strong promoter of the duodecimal system, has said that duodecimal tables are easier to master than decimal ones, which makes elementary teaching more interesting. He also stated that students will be able to do mathematical calculations one and a half times faster when compared to the decimal system.
  • There exist mathematicians and organizations like the Dozenal Society of Great Britain and America, which promote that we should count in 12s rather than 10s, as they claim that it would be more efficient in day-to-day life.

Disadvantages of the base-12 number system

Even though the duodecimal system has many advantages and approvals from many mathematicians, it's not practical for everyday use.

  • The main reason is that the majority of the people are used to the decimal system, and it will create confusion as people are not familiar with the base-12 system.
  • Another reason is that most technical equipment's including computers and calculators are built for decimal numbers, therefore they won't work well base-12 system.
  • The extra two symbols, A and B, are different in different cultures, which again makes confusion, as there is no standard way of representing them.
  • Conversion between base-12 and other number systems like base-10 requires extra steps, which make them less practical for daily use.

Duodecimal in Java

Even though Java does not support base-12 numbering directly, we can represent them by storing them as a string and converting them into a familiar number system like the decimal system (base-10). We can convert decimal number to duodecimal by repeatedly dividing the digits by 12 and recording the remainders which corresponds to duodecimal digits. We can also convert duodecimal to decimal by iterating through each digit and calculating its decimal value based on the position by multiplying by 12 and adding them together. Therefore, even though Java does not provide inbuilt functionalities for the duodecimal system like other number systems like octal, hexadecimal, and binary have, we can still represent them with custom-built code.

Converting Decimal to Duodecimal

We can convert a decimal number to a duodecimal number using the following steps ?

  • Divide the decimal number by 12 and find its remainder and quotient.
  • Divide the quotient by 12 and find its remainder and quotient.
  • Repeat these steps till the quotient becomes 0.
  • Now write down the remainders we found in each step in reverse order.

Example

We can understand it better with the help of an example. Suppose we have to convert decimal number 50 to duodecimal.

1.	First divide 50 by 12:
50/12=4
remainder =2
2.	Divide the quotient by 12:
4/12=0
remainder=4
3.	As the quotient becomes zero, we stop here. 
4.	Now write the remainders in reverse order. So we get the answer as 42.

Algorithm

Step 1 ? Input a decimal number. If the number is zero, return zero.

Step 2 ? Initialize StringBuilder to create the duodecimal representation.

Step 3 ? Create an array containing characters [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B], which represent base-12 digits.

Step 4 ? Create a while loop with the condition 'number is greater than 0'.

Step 5 ? Find the remainder by using the modulus operator when divided by 12. (i.e., number % 12)

Step 6 ? With the help of the array created before, we can fetch the corresponding character using the remainder as an index. After getting the character, we append it to the StringBuilder.

Step 7 ? Now divide the number by 12.

Step 8 ? Repeat steps from 5-7 till the condition in the while loop remains true.

Step 9 ? Now reverse the StringBuilder of remainders using StringBuilder.reverse() method.

Step 10 ? Convert the reversed StringBuilder to a string and return it.

Implementation code

The following Java code converts a decimal number to a duodecimal number.

import java.util.Scanner;
public class DecimalToDuodecimal {
   public static String convertToDuodecimal(int decimal) {
      if (decimal == 0) {
         return "0";
      }
      StringBuilder duodecimal = new StringBuilder();
      char[] duodecimalDigits = "0123456789AB".toCharArray(); // A = 10, B = 11
      while (decimal > 0) {
         int remainder = decimal % 12;
         duodecimal.append(duodecimalDigits[remainder]);
         decimal /= 12;
      }
      return duodecimal.reverse().toString();
   }
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter a decimal number: ");
      int decimal = scanner.nextInt();
      String duodecimal = convertToDuodecimal(decimal);
      System.out.println("Duodecimal representation: " + duodecimal);
      scanner.close();
   }
}

Output

Enter a decimal number: 100
Duodecimal representation: 84
Enter a decimal number: 18342
Duodecimal representation: A746

Converting Duodecimal to Decimal

We can convert a duodecimal number to a decimal number using the following steps ?

  • Identify the duodecimal digits and convert to corresponding decimal digits, like A to 10 and B to 11.
  • Multiply each digit by 12 raised to the power of its position from the right.
  • 3. Add the results together to get the decimal value.

Example

We can understand it better with the help of an example. Suppose we have to convert the duodecimal number 1B5 to decimal.

B in duodecimal represents 11 in decimal. So the answer will be:
1 * (12^2) + 11* (12^1) + 5 * (12^0)  
= 1*144 + 11*12 + 5*1
= 144 + 132 + 5
= 281
The duodecimal number 1B5 in the decimal system is 281.

Algorithm

Step 1 ? Input a duodecimal number as a string.

Step 2 ? Initialize a variable named decimal to store the result and another variable named power to keep track of the power of 12.

Step 3 ? Loop through the digits from right to left.

Step 4 ? If the digit is from 0-9, assign the same value; otherwise, if it is A or B, use 10 and 11, respectively.

Step 5 ? Multiply this value by 12 to the power, which is 0 initially. Add this result to the decimal variable.

Step 6 ? Increment the power by one.

Step 7 ? Repeat it for all the digits.

Step 8 ? Now print the final result.

Implementation code

The following Java code converts a duodecimal number to a decimal number.

import java.util.Scanner;
public class duotodec{
   public static int convertToDecimal(String duodecimal) {
      int decimal = 0;
      int power = 0; 
      for (int i = duodecimal.length() - 1; i >= 0; i--) {
         char digit = duodecimal.charAt(i);
         int value;
         if (Character.isDigit(digit)) {
            value = Character.getNumericValue(digit);
         } else if (digit == 'A' || digit == 'a') {
            value = 10;
         } else if (digit == 'B' || digit == 'b') {
            value = 11;
         } else {
            System.out.println("Invalid duodecimal digit: " + digit);
            return -1; 
         }
         decimal += value * Math.pow(12, power); 
         power++; 
      }
      return decimal;
   }
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter a duodecimal number: ");
      String duodecimal = scanner.nextLine();
      int decimal = convertToDecimal(duodecimal);
      System.out.println("Decimal representation: " + decimal);
      scanner.close();
   } 
}

Output

Enter a duodecimal number: 1B5
Decimal representation: 281

Enter a duodecimal number: 2AA1
Decimal representation: 5017
Updated on: 2025-02-28T11:27:37+05:30

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements