File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ The equivalent Decimal Number : 485
10
10
+ 1. Figure out howo to convert octal to decimal
11
11
745 = (7 * 8^2) + (4 * 8^1) + (5 * 8^0) = 485
12
12
+ 1.5 Input
13
- 2. Pow function
13
+ + 2. Pow function
14
14
3. Function to count amount of digits
15
15
4. Write function to iterate over digits and return sum of powers
16
16
+ 5. Invalid input
@@ -28,17 +28,30 @@ enum boolean_for_validation {
28
28
29
29
int input_octal (int * octal );
30
30
void print_invalid_input ();
31
+ int power (int base , int exponent );
31
32
32
33
int main () {
33
34
int octal = 0 ;
34
35
if (input_octal (& octal )) {
35
- printf ("%d" , octal );
36
+ printf ("%d" , power ( octal , 0 ) );
36
37
} else {
37
38
print_invalid_input ();
38
39
}
39
40
return 0 ;
40
41
}
41
42
43
+ int power (int base , int exponent ) {
44
+ int result = base ;
45
+ if (exponent == 0 ) {
46
+ result = 1 ;
47
+ } else {
48
+ for (int i = 2 ; i <= exponent ; ++ i ) {
49
+ result *= base ;
50
+ }
51
+ }
52
+ return result ;
53
+ }
54
+
42
55
void print_invalid_input () {
43
56
printf ("n/a" );
44
57
}
You can’t perform that action at this time.
0 commit comments