C program to convert hexadecimal into octal.

 

01 #include <stdio.h>
02 #include <math.h>
03 #include <string.h>
04
05 int main()
06 {
07 char hex[17];
08
09 long int decimal=0, place=1;
10
11 int i = 0, val, len,octal=0,remainder;
12
13 printf("Enter any hexadecimal number: ");
14 gets(hex);
15
16 /* length of hex digit */
17
18 len = strlen(hex);
19 len--;
20
21 /*hexa to decimal*/
22
23 for(i=0; hex[i]!=''; i++)
24 {
25 switch(hex[i])
26 {
27 case '0':
28 val = 0;
29 break;
30 case '1':
31 val = 1;
32 break;
33 case '2':
34 val = 2;
35 break;
36 case '3':
37 val = 3;
38 break;
39 case '4':
40 val = 4;
41 break;
42 case '5':
43 val = 5;
44 break;
45 case '6':
46 val = 6;
47 break;
48 case '7':
49 val = 7;
50 break;
51 case '8':
52 val = 8;
53 break;
54 case '9':
55 val = 9;
56 break;
57 case 'a':
58 case 'A':
59 val = 10;
60 break;
61 case 'b':
62 case 'B':
63 val = 11;
64 break;
65 case 'c':
66 case 'C':
67 val = 12;
68 break;
69 case 'd':
70 case 'D':
71 val = 13;
72 break;
73 case 'e':
74 case 'E':
75 val = 14;
76 break;
77 case 'f':
78 case 'F':
79 val = 15;
80 break;
81 }
82
83 decimal += val * pow(16, len);
84 len--;
85 }
86
87 /*now decimal to octal*/
88
89 i=1;
90 while(decimal != 0)
91 {
92 remainder = decimal%8;
93 octal = octal + (remainder*i);
94 decimal = decimal/8;
95 i = i*10;
96 }
97
98 printf("Octal Number = %d",octal);
99 return 0;
100 }
 
 OUTPUT :

Enter any hexadecimal number : A1
Octal Number = 241

Related Post

Leave a Reply

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