2020 * the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
2121 * It also requires a dot (decimal point) to
2222 * represent decimal fractions.
23+ *
24+ * Steps
25+ * ------
26+ * - get input (hexadecimal) from the user
27+ * - create a variable, lets say pow (default value is 0)
28+ * - create a variable, lets say decimal (default value is 0)
29+ * - get the last digit of the octal number (last_digit = num%10)
30+ * - multiply the last digit with power of 8 to pow variable
31+ * - add the above result with decimal variable
32+ * - increment the pow variable
33+ * - continue all the above steps for all the digits of octal
34+ *
35+ * say the octal is = 123
36+ * decimal = 1 * 8^2 + 2 * 8^1 + 3 * 8^0
37+ * decimal = 1 * 64 + 2 * 8 + 3 * 1
38+ * decimal = 64 + 16 + 3
39+ * decimal = 83
40+ *
41+ * say the octal is = 144
42+ * decimal = 1 * 8^2 + 4 * 8^1 + 4 * 8^0
43+ * decimal = 1 * 64 + 4 * 8 + 4 * 1
44+ * decimal = 64 + 32 + 4
45+ * decimal = 100
46+ *
2347 */
48+
2449public class OctalToDecimal {
2550 public static void main (String [] args ) {
51+ // int octal = 123;
52+ // int octal = 144;
53+ int octal = 1000 ;
54+ System .out .println ("The Octal Number is : " +octal );
55+ int pow = 0 ;
56+ int decimal = 0 ;
2657
58+ while (octal > 0 ){
59+ int unitDigit = octal % 10 ;
60+ decimal = (int ) (decimal + (unitDigit *Math .pow (8 , pow )));
61+ pow ++;
62+ octal = octal / 10 ;
63+ }
64+ System .out .println ("The Decimal Value is : " +decimal );
2765 }
2866}
2967/*
3068 OUTPUT
69+
70+ The Octal Number is : 123
71+ The Decimal Value is : 83
72+
73+ The Octal Number is : 144
74+ The Decimal Value is : 100
75+
76+ The Octal Number is : 1000
77+ The Decimal Value is : 512
3178*/
0 commit comments