Give an example of octal to decimal conversation using Java.
easy method
Answers
Answer:
the following example, we have an octal value stored in a string variable onum and we are converting it to a decimal value using Integer. parseInt() method. This method accepts String as an argument and converts it into a decimal value based on the base value provided along with the string as argument.
Here is your code.
public class X
{
public static int getDecimal(int octal)
{
int decimal = 0;
int n = 0;
while(true)
{
if(octal == 0)
{
break;
}
else
{
int temp = octal%10;
decimal += temp*Math.pow(8, n);
octal = octal/10;
n++;
}
}
return decimal;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt();
System.out.println("Decimal of "+n+"is:"+getDecimal(n));
}
}