Question
Program to convertĀ octal to decimal
ENTER OCTAL NUMBER:172
THE DECIMAL VALUE OF OCTAL 172 IS 122
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class OctalToDecimal
{
public static void main()
{
int count=0;
Scanner sc=new Scanner(System.in);
long octal=0,decimal=0,temp=0;
boolean valid=true;
System.out.print("ENTER OCTAL NUMBER:");
octal=sc.nextInt();
temp=octal;
count=0;
while(temp>0)
{
if(temp%10>7)
{
System.out.println("Invalid Octal Number");
valid=false;
break;
}
temp=temp/10;
}
if(valid==true)
{
temp=octal;
while(temp>0)
{
decimal=decimal+(temp%10)*((long)Math.pow(8,count));
count++;
temp=temp/10;
}
System.out.println("THE DECIMAL VALUE OF OCTAL "+octal+" IS "+decimal);
}
}
}