This Java program takes a hexadecimal number as input from the user and converts it into its octal equivalent using the method hex_to_octal().
The program starts by taking the input hexadecimal number as a string using the Scanner class. It then uses a loop to iterate through each character of the string and converts each hexadecimal digit into its decimal equivalent using a switch statement.
Once the hexadecimal number is converted to decimal, the program uses another loop to convert the decimal number into its octal equivalent. This is done by repeatedly dividing the decimal number by 8 and concatenating the remainder to the beginning of the output string.
The program then outputs the input hexadecimal number and its corresponding octal number using System.out.println() statements. Overall, this program is a simple implementation of a hexadecimal to octal converter in Java.
import java.util.Scanner; class Hexa_to_Octal { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Hexdecimal Number : "); String hex = input.nextLine(); int dec = 0; int c = hex.length() - 1; for(int i = 0; i < hex.length() ; i ++ ) { char ch = hex.charAt(i); switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': dec = dec + Integer.parseInt(Character.toString(ch))*(int)Math.pow(16,c); c--; break; case 'a': case 'A': dec = dec + 10 * (int)Math.pow(16, c); c--; break; case 'b': case 'B': dec = dec + 11 * (int)Math.pow(16, c); c--; break; case 'c': case 'C': dec = dec + 12 * (int)Math.pow(16, c); c--; break; case 'd': case 'D': dec = dec + 13 * (int)Math.pow(16, c); c--; break; case 'e': case 'E': dec = dec + 14 * (int)Math.pow(16, c); c--; break; case 'f': case 'F': dec = dec + 15 * (int)Math.pow(16, c); c--; break; default: System.out.println("Invalid Hexdecimal Number"); break; } } String oct =""; while(dec > 0) { oct = dec % 8 + oct; dec = dec / 8; } System.out.println("Hexdecimal Number : "+hex); System.out.println("Octal Number : "+oct); } }
Enter Hexdecimal Number : 1AC Hexdecimal Number : 1AC Octal Number : 654
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions