Skip to content

Commit ad6a027

Browse files
committed
Flipped the resulring number
1 parent a95924b commit ad6a027

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

For loop/50. Decimal to octal.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The Octal of 79 is 117.
2424
+ Function to count how many iterations of /8 there will be
2525
+ Power function
2626
+ Function to get the number int
27-
Reverse it
27+
+ Reverse it
2828
5. Output
2929
6. Test
3030
7. Cpplint test
@@ -37,25 +37,44 @@ int input_decimal_number();
3737
int number_of_eights(int decimal_number);
3838
int power(int base, int exponent);
3939
int convert(int decimal_number, int division_by_eight);
40+
int reverse_number(int converted_number, int division_by_eight);
4041

4142
int main() {
4243
int decimal_number = input_decimal_number();
4344
int division_by_eight = number_of_eights(decimal_number);
4445
int converted = convert(decimal_number, division_by_eight);
46+
int converted_and_flipped = reverse_number(converted, division_by_eight);
4547
printf("Division by eight = %d\n", division_by_eight);
46-
printf("Converted but not flipped = %d", converted);
48+
printf("Converted but not flipped = %d, flipped = %d\n", converted, converted_and_flipped);
4749
return 0;
4850
}
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-
// 79 % 8 == 7; quotient = 9
55-
// 9 % 8 == 1; q = 1
56-
// 1 % 8 == 1; q = 1
57-
// I will probably need a function to count how many iterations I will need to convert result into a number.
58-
// I'll need that function once again when I will flip the number
51+
52+
// 711 -> 117
53+
54+
int reverse_number(int converted_number, int division_by_eight) {
55+
int result = 0, temp_converted = converted_number, digit, power_counter = division_by_eight - 1;
56+
for (int i = 0; i <= division_by_eight; ++i) {
57+
digit = temp_converted % 10;
58+
temp_converted /= 10;
59+
result += digit * power(10, power_counter);
60+
--power_counter;
61+
}
62+
return result;
63+
}
64+
65+
/*
66+
Conversion steps:
67+
1 Divide the number by 8.
68+
2 Get the integer quotient for the next iteration.
69+
3 Get the remainder for the octal digit.
70+
4 Repeat the steps until the quotient is equal to 0.
71+
79 % 8 == 7; quotient = 9
72+
9 % 8 == 1; q = 1
73+
1 % 8 == 1; q = 1
74+
I will probably need a function to count how many iterations I will need to convert result into a number.
75+
I'll need that function once again when I will flip the number
76+
*/
77+
5978
int convert(int decimal_number, int division_by_eight) {
6079
int result = 0, quotient, remainder, temp_decimal = decimal_number, divider = 8, power_counter = division_by_eight;
6180
for (int i = 0; i <= division_by_eight; ++i) {

0 commit comments

Comments
 (0)