File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments