Powered by Blogger.

Program to Convert Decimal to Octal,Hexa and Binary values

>> Saturday, October 18, 2014

Binary, Hexa and Octal are the number formats to use in different mathematical calculations. In our real life we use numbers with decimals. It may be integer value or float value both are called as decimals. Let us know the number format examples given below:
Decimal format : A representation of a real number using the base ten and decimal notation.
program to convert decimal to octa-hexa-binary_JavabynataraJ
Binary format : Binary numbers contains only zero and one.
Ex: Decimal 25 is binary 11001
Octal format: Octa is the base-8 number system, and uses the digits 0 to 7.
Ex: Decimal 25 is octa 31
Hexadecimal format: Hexadecimal is a base of 16. This uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a–f) to represent values ten to fifteen.
Ex: Decimal 25 is Hexa 19
In our program i am using the Scanner class to read data dynamically from the keyboard. I have used different methods convert in different formats using integer value.
The methods i have used: toOctalString(integer),toHexString(integer),toBinaryString(integer).
Read the int value using sc.nextInt() method using util.Scanner, then convert it into Integer wrapper class type using Integer.valueOf(i).
Java Program to convert Decimal value to Octa,Hexa and Binary values:

package com.javabynataraj.core;

import java.util.Scanner;
//http://javabynataraj.blogspot.com
public class IntegerConversion {
 public static void main(String[] args) {
 //user Scanner class to read data from keyboard
 Scanner sc = new Scanner(System.in);
 System.out.println("Enter the decimal number:");
 int i = sc.nextInt();
 Integer io = Integer.valueOf(i);
 System.out.println("OctaDecimal for "+i+" is "+io.toOctalString(i));
 System.out.println("Hexa Decimal for "+i+" is "+io.toHexString(i));
 //Calling the method with class name support
 String st = Integer.toBinaryString(i);
 System.out.println("Binary Decimal for "+i+" is "+st);
 
 }
}
Open your command prompt and compile the given program using as shown in the below window. and run with fully qualified class name.

Output:

Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.