Skip to content

Commit caacdaf

Browse files
committed
Digits counter done
1 parent 45cd92b commit caacdaf

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

For loop/51. Octal to decimal.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The equivalent Decimal Number : 485
1111
745 = (7 * 8^2) + (4 * 8^1) + (5 * 8^0) = 485
1212
+ 1.5 Input
1313
+ 2. Pow function
14-
3. Function to count amount of digits
14+
+ 3. Function to count amount of digits
1515
4. Write function to iterate over digits and return sum of powers
1616
+ 5. Invalid input
1717
6. Test
@@ -29,17 +29,26 @@ enum boolean_for_validation {
2929
int input_octal(int *octal);
3030
void print_invalid_input();
3131
int power(int base, int exponent);
32+
int find_number_of_digits(int octal);
3233

3334
int main() {
3435
int octal = 0;
3536
if (input_octal(&octal)) {
36-
printf("%d", power(octal, 0));
37+
printf("%d\n", find_number_of_digits(octal));
3738
} else {
3839
print_invalid_input();
3940
}
4041
return 0;
4142
}
4243

44+
int find_number_of_digits(int octal) {
45+
int counter = 0;
46+
for (int i = octal; i > 0; i /= 10) {
47+
++counter;
48+
}
49+
return counter;
50+
}
51+
4352
int power(int base, int exponent) {
4453
int result = base;
4554
if (exponent == 0) {

0 commit comments

Comments
 (0)