Java program for octal to binary conversion.



package demo;
import java.util.Scanner;
public class OctalToBinary {
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int[] octalvalues = {0, 1, 10, 11, 100, 101, 110, 111};
  long octal, tempOctal, binary, place;
  int rem;
  /*
   * Reads Octal number from user
   */
  System.out.print("Enter any Octal number: ");
  octal = sc.nextLong();
  tempOctal = octal;
  binary = 0;
  place = 1;
  /*
   * Finds Binary of the octal number
   */
  while (tempOctal != 0)
  {
   rem = (int)(tempOctal % 10);
   binary = octalvalues[rem] * place + binary;
   tempOctal /= 10;
   place *= 1000;
  }
  System.out.println("Octal number = " + octal);
  System.out.print("Binary number = " + binary);
 }
}

Output:
Enter any Octal number: 1250
Octal number = 1250
Binary number = 1010101000
BUILD SUCCESSFUL (total time: 3 seconds)


No comments:

Post a Comment