Skip to content

Commit f0fcb09

Browse files
Merge pull request #180 from sonal10red100/master
Create binary_to_octal.c
2 parents 272ccfb + 5e926bb commit f0fcb09

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

binary_to_octal.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <math.h>
2+
#include <stdio.h>
3+
4+
int convert_to_octal(long long binary);
5+
6+
int main() {
7+
long long binary;
8+
printf("Enter a binary number: ");
9+
scanf("%lld", &binary);
10+
printf("%lld in binary = %d in octal", binary, convert(binary));
11+
return 0;
12+
}
13+
14+
int convert_to_octal(long long binary) {
15+
int octal = 0, decimal = 0, i = 0;
16+
17+
//Step 1> convert binary to decimal
18+
//Step 2> convert decimal to octal
19+
20+
// converting binary to decimal
21+
while (binary != 0) {
22+
decimal += (binary % 10) * pow(2, i);
23+
++i;
24+
binary /= 10;
25+
}
26+
i = 1;
27+
28+
// converting to decimal to octal
29+
while (decimal != 0) {
30+
octal += (decimal % 8) * i;
31+
decimal /= 8;
32+
i *= 10;
33+
}
34+
return octal;
35+
}

0 commit comments

Comments
 (0)