Skip to content

Commit f06711c

Browse files
committed
Midway done with the exercise
1 parent 58b6211 commit f06711c

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

For loop/50. Decimal to octal.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,24 @@ Expected Output :
77
The Octal of 79 is 117.
88
99
+ 1. Take input
10-
2. Figure out how to convert to octal
10+
+ 2. Figure out how to convert to octal:
11+
Devide the decimal by 8 and take the quotient to devide until it's zero
12+
Put that remainder at the end of the octal number;
13+
79 % 8 == 7; quotient = 9
14+
9 % 8 == 1; q = 1
15+
1 % 8 == 1; q = 1
16+
117
17+
Conversion steps:
18+
1 Divide the number by 8.
19+
2 Get the integer quotient for the next iteration.
20+
3 Get the remainder for the octal digit.
21+
4 Repeat the steps until the quotient is equal to 0.
1122
3. Invalid input
12-
4. Convert
23+
4. Convert:
24+
+ Function to count how many iterations of /8 there will be
25+
+ Power function
26+
Function to get the number int
27+
Reverse it
1328
5. Output
1429
6. Test
1530
7. Cpplint test
@@ -19,11 +34,54 @@ The Octal of 79 is 117.
1934
#include <stdio.h>
2035

2136
int input_decimal_number();
37+
int number_of_eights(int decimal_number);
38+
int power(int base, int exponent);
39+
int convert(int decimal_number);
2240

2341
int main() {
2442
int decimal_number = input_decimal_number();
43+
int division_by_eight = number_of_eights(decimal_number);
44+
// int converted = convert(decimal_number);
45+
printf("%d\n", division_by_eight);
46+
printf("%d", power(5, 0));
2547
return 0;
2648
}
49+
// Conversion steps:
50+
// 1 Divide the number by 8.
51+
// 2 Get the integer quotient for the next iteration.
52+
// 3 Get the remainder for the octal digit.
53+
// 4 Repeat the steps until the quotient is equal to 0.
54+
// I will probably need a function to count how many iterations I will need to convert result into a number.
55+
// I'll need that function once again when I will flip the number
56+
int convert(int decimal_number) {
57+
return decimal_number;
58+
}
59+
60+
int power(int base, int exponent) {
61+
int result;
62+
if (exponent == 0) {
63+
result = 1;
64+
} else {
65+
result = base;
66+
for (int i = 2; i <= exponent; ++i) {
67+
result *= base;
68+
}
69+
}
70+
return result;
71+
}
72+
73+
int number_of_eights(int decimal_number) {
74+
int divisor = 8, quotient, counter = 0, temp_decimal = decimal_number;
75+
for (int i = 0; i <= decimal_number; ++i) {
76+
quotient = temp_decimal / divisor;
77+
temp_decimal = quotient;
78+
++counter;
79+
if (quotient == 0) {
80+
break;
81+
}
82+
}
83+
return counter;
84+
}
2785

2886
int input_decimal_number() {
2987
int decimal_number;

0 commit comments

Comments
 (0)