Java program for octal to decimal.



package demo;
import java.util.Scanner;
public class OctalToDecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  long octal, decimal = 0;
  int i = 0;
  System.out.print("Enter any octal number: ");
  octal = sc.nextLong();
  while (octal != 0) 
  {
   decimal = (long)(decimal + (octal % 10) * Math.pow(8, i++));
   octal = octal / 10;
  }
  System.out.print("Equivalent decimal value: " + decimal);
 }
}

Output:
Enter any octal number: 1000
Equivalent decimal value: 512
BUILD SUCCESSFUL (total time: 3 seconds)


No comments:

Post a Comment