Skip to content

Commit 45cd92b

Browse files
committed
Power function done
1 parent cc4915b commit 45cd92b

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

For loop/51. Octal to decimal.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The equivalent Decimal Number : 485
1010
+ 1. Figure out howo to convert octal to decimal
1111
745 = (7 * 8^2) + (4 * 8^1) + (5 * 8^0) = 485
1212
+ 1.5 Input
13-
2. Pow function
13+
+ 2. Pow function
1414
3. Function to count amount of digits
1515
4. Write function to iterate over digits and return sum of powers
1616
+ 5. Invalid input
@@ -28,17 +28,30 @@ enum boolean_for_validation {
2828

2929
int input_octal(int *octal);
3030
void print_invalid_input();
31+
int power(int base, int exponent);
3132

3233
int main() {
3334
int octal = 0;
3435
if (input_octal(&octal)) {
35-
printf("%d", octal);
36+
printf("%d", power(octal, 0));
3637
} else {
3738
print_invalid_input();
3839
}
3940
return 0;
4041
}
4142

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+
4255
void print_invalid_input() {
4356
printf("n/a");
4457
}

0 commit comments

Comments
 (0)