This Java program takes a hexadecimal number as input from the user and converts it into its decimal equivalent using the method hexa_to_decimal(). The Scanner class is used to take input from the user, and the nextLine() method is used to read the input as a string.
The hexa_to_decimal() method takes the hexadecimal number as input and converts it into its decimal equivalent. It does this by iterating through the hexadecimal number from right to left and multiplying each digit by the appropriate power of 16 based on its position.
The program then outputs the input hexadecimal number and its corresponding decimal number using System.out.println() statements. Overall, this program is a simple implementation of a hexadecimal to decimal converter in Java.
import java.util.Scanner; class Hexa_Decimal { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Hexadecimal Number : "); String hex = input.nextLine(); System.out.println("Hexadecimal Number :"+hex); System.out.println("Decimal Number :"+hexa_to_decimal(hex)); } static int hexa_to_decimal(String hex_num) { int len = hex_num.length(); int base = 1; int dec_val = 0; for (int i = len - 1; i >= 0; i--) { if (hex_num.charAt(i) >= '0' && hex_num.charAt(i) <= '9') { dec_val += (hex_num.charAt(i) - 48) * base; base = base * 16; } else if (hex_num.charAt(i) >= 'A' && hex_num.charAt(i) <= 'F') { dec_val += (hex_num.charAt(i) - 55) * base; base = base * 16; } } return dec_val; } }
Enter Hexadecimal Number : 7CA Hexadecimal Number :7CA Decimal Number :1994
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions