Proxy > Gmail Facebook Yahoo!

Java program to to convert Octal number into Decimal number



Octal to Decimal: How to Convert
Octal to decimal conversion is one of the most commonly taught problem solving exercises in computer basics. So is there an octal to decimal formula? Yes, an octal number can be converted to a decimal number using the following formula:

Decimal Form = Ʃ(ai x 8i)

In this formula, ‘a’ is the individual digit being converted, while ‘i‘ is the number of the digit counting from the right-most digit in the number, the right-most digit starting with 0.
For example:
Convert (765)8 into decimal:
(765)8 = (7 x 82) + (6 x 81) + (5 x 80)
= (7 x 64) + (6 x 8 ) + (5 x 1)
= 448 + 48 + 5
= 501
Hence (765)8 = (501)10
Similarly,
(1336)8 = (734)10
(5467)8 = (2871)10
(6345)8 = (3301)10(
(76534)8 = (32092)10
(724635)8 = (240029)10
Try out yourselves!
How the Program works?
This program accepts the octal number from the user, saves individual digits to individual spaces in the array in the 1st ‘for’ loop and applies the above formula on them in the second ‘for’ loop.

import java.util.Scanner;
public class OctalToDecimal{
  static Scanner MyScan = new Scanner(System.in);
  public static void main(String args[])
  {
    int arr[] = new int[20];
    int DecNum=0,i=0;
    System.out.println("Enter the octal number: ");
    int num = MyScan.nextInt();
    for(i=0; num>0; i++) //Save the individual digits of the Octal number to the array.
    {
      arr[i] = num % 10;
      num = num / 10;
    }
    for(int power=0, j=0; j<i ; j++,power++)
    {
      DecNum = (int) (DecNum + arr[j] * Math.pow(8,power));
    }
    System.out.println("Decimal number: "+ DecNum);
  }
}


Responses

0 Respones to "Java program to to convert Octal number into Decimal number"


Send mail to your Friends.  

Expert Feed

 
Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar