C program to convert binary into octal.

 

01  #include <stdio.h>
02
03 int main()
04 {
05 long binary;
06 int remainder;
07 long decimal = 0, octal = 0, i = 1;
08
09 printf("Enter a binary number : ");
10 scanf("%ld", &binary);
11
12
13 /* Convert Binary to Decimal number first */
14
15
16 while(binary != 0)
17 {
18 remainder = binary % 10;
19 decimal = decimal + (remainder * i);
20 binary = binary / 10;
21 i = i * 2;
22 }
23
24 /*Then convert decimal to octal number */
25
26 i = 1;
27
28 while(decimal != 0)
29 {
30 remainder = decimal%8;
31 octal = octal + (remainder*i);
32 decimal = decimal/8;
33 i = i*10;
34 }
35
36 printf("Octal number : %d", octal);
37
38 return 0;
39 }
 
 OUTPUT :

Enter a Binary Number : 1010
Octal number : 12

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *