on Leave a Comment

Java Program to Convert Octal Number to Decimal Number

This java program converts octal value to decimal value. First user enters any octal number and octal value is converted into decimal value and finally decimal equivalent is displayed on the screen.

Octal Number: The octal number system is the base-8 number system, and uses the digits 0 to 7.

Java Program to Convert Octal Number to Decimal Number

import java.util.Scanner;

public class OctToDec
{
    public static void main(String args[])
    {
        int octal, decimal=0, i=0;
        Scanner scan = new Scanner(System.in);
  
        System.out.print("Enter any Octal Number : ");
        octal = scan.nextInt();
  
        for( ; octal != 0; i++, octal = octal/10)
        {
            decimal = decimal + (octal%10) * (int) Math.pow(8, i);
        }
  
        System.out.print("Decimal Equivalent is " + decimal);
    }
}

Output:

Enter any Octal Number : 246
Decimal Equivalent is 166

0 comments:

Post a Comment