×

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers to their appropriate binary, octal, decimal, hexadecimal, or other base numbers. To do the following conversion in Java, we can utilize existing packages or user-defined methods.

Steps to find Duodecimal Number:

To find the duodecimal number of any given number, perform the following steps:

  • As input, take a hexadecimal number from the user.
  • Create a custom method to convert it to decimal.
  • We also write a user-defined function that converts decimal numbers to duodecimal numbers.
  • Return the duodecimal number.

DuoDecimalDemoExample.java

import java.util.*;  
class DuoDecimalDemoExample {  
// To discover the equivalent duodecimal number of a given hexadecimal number , define the class DuodecimalDemoExample. 
    public static String convToDeci (String val , int bas , Map<Character , Integer> hexToDec)  
// construct a convToDeci () method that returns the decimal equivalent of the given hexadecimal value
    {  
        int add = 0;  
        int pow = 0;  
        String tem_Data = val;  
          // finding corresponding decimal number 
       
        for (int i = tem_Data.length () - 1; i >= 0; i--) {  
            int value = tem_Data.charAt (i) - '0';  
            if (bas == 16 && hexToDec.containsKey (tem_Data.charAt (i))) {  
                value = hexToDec.get (tem_Data.charAt (i));  
            }  
            add += (value) * (Math.pow (bas, pow++));  
        }   
        return String.valueOf (add);  
    }  
  
    public static String convertToDuoDecimal (String val , int bas , Map<Integer , Character>dectoHex , Map<Character , Integer>hextoDec)  
// develop a convertToDuoDecimal () method that converts a decimal number to a Duodecimal value and returns it to the main () method
    {  
        String val1 = val;  
        int nBase = bas;  
  // verify base is decimal or not 
         
        if (nBase != 10) {  
            // If the base is not 10, it calls the convToDeci () function, which returns the appropriate decimal number of the provided number.
            val = convToDeci (val, bas, hextoDec);  
              
             //  We update the value of newBase to 10 after converting the number.
            nBase = 10;  
        }  
          
        
        int tempo = Integer.parseInt (val1);  
        int rem;    
        String duoDecimal = "";     
          
      
        char duoChars [] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'};    
           
        while (tempo > 0)    
         {    
           rem = tempo % 12;     
           duoDecimal = duoChars [rem]+duoDecimal;     
           tempo = tempo / 12;    
         }    
         return duoDecimal;  
    }  
      
    public static void main (String [] args)  
    {  
      
        String val1;  
          
        
        Scanner sc0 = new Scanner (System.in);  
        System.out.print ("enter hexadecimal number: ");  
        val1 = sc0.nextLine ();  
          
        
        sc0.close ();  
          


        Map <Character , Integer> hexToDec = new HashMap<> ();  
        Map <Integer , Character> dectoHex = new HashMap<> ();  
          
       
        for (int i = 0; i < 6; i++) {  
            dectoHex.put (10 + i , (char) ('A' + i));  
            hexToDec.put ((char) ('A' + i) ,  10 + i);  
        }  
          
          
        System.out.println ("Duodecimal : " + convertToDuoDecimal (val1 , 16 , dectoHex , hexToDec));  
          
          
    }  
}  

Output:

Duodecimal in Java

Other than hexadecimal, we must construct user-defined methods such as the convertToDec () function, which will convert the supplied integer into decimal and return it for calculating the duodecimal number.


Related Topics

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Compare Two Times in Java

Definition The compareTo() function of the LocalTime class is used to compare times. The class's compareTo() method compares two LocalTime objects. Here we have two objects that have to be compared: the...

4 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.