Octal to decimal conversion program in java. For example 8 is decimal representation of octal number 10.
import java.util.Scanner; public class Conversion { static int power(int pos,long count) { int Mfactor=1; while(count>0) { Mfactor=Mfactor*pos; count=count-1; } return Mfactor; } public static void main(String[] args) { long Number,Remainder,Decimal=0,count=0; Scanner input=new Scanner(System.in); System.out.print("enter Octal Number:"); Number=input.nextInt(); input.close(); while(Number!=0) { Remainder=Number%10; Decimal=Decimal+Remainder*power(8,count); count=count+1; Number=Number/10; } System.out.println("Decimal representation:"+Decimal); } }
OUTPUT enter Octal Number:15 Decimal representation:13 ------------------------- enter Octal Number:50 Decimal representation:40 ------------------------- enter Octal Number:100 Decimal representation:64