@@ -12,7 +12,7 @@ The equivalent Decimal Number : 485
12
12
+ 1.5 Input
13
13
+ 2. Pow function
14
14
+ 3. Function to count amount of digits
15
- 4. Write function to iterate over digits and return sum of powers
15
+ + 4. Write function to iterate over digits and return sum of powers
16
16
+ 5. Invalid input
17
17
6. Test
18
18
7. Cpplint test
@@ -30,17 +30,30 @@ int input_octal(int *octal);
30
30
void print_invalid_input ();
31
31
int power (int base , int exponent );
32
32
int find_number_of_digits (int octal );
33
+ int find_decimal (int octal , int number_of_digits );
33
34
34
35
int main () {
35
36
int octal = 0 ;
36
37
if (input_octal (& octal )) {
37
- printf ("%d\n" , find_number_of_digits (octal ));
38
+ int number_of_digits = find_number_of_digits (octal );
39
+ printf ("%d" , find_decimal (octal , number_of_digits ));
38
40
} else {
39
41
print_invalid_input ();
40
42
}
41
43
return 0 ;
42
44
}
43
45
46
+ int find_decimal (int octal , int number_of_digits ) {
47
+ int decimal = 0 , temp_octal = octal , power_of_ten ;
48
+ for (int i = number_of_digits ; i > 0 ; -- i ) {
49
+ power_of_ten = power (10 , number_of_digits - 1 );
50
+ decimal += temp_octal / power_of_ten * power (8 , number_of_digits - 1 );
51
+ temp_octal -= temp_octal / power_of_ten * power_of_ten ;
52
+ -- number_of_digits ;
53
+ }
54
+ return decimal ;
55
+ }
56
+
44
57
int find_number_of_digits (int octal ) {
45
58
int counter = 0 ;
46
59
for (int i = octal ; i > 0 ; i /= 10 ) {
0 commit comments