C program to convert octal into hexadecimal.

 

01  #include<stdio.h>
02 #include<math.h>
03 #include<string.h>
04
05 int main()
06 {
07 char hex[20],c; int n;
08 int i=0,decimal=0, rem;
09
10 printf("Enter octal number : ");
11 scanf("%d",&n);
12
13 /*octal to decimal*/
14
15 while (n!=0)
16 {
17 rem = n%10;
18 n=n/10;
19 decimal=decimal+rem*pow(8,i);
20 ++i;
21 }
22
23 /*now decimal to hexdecimal*/
24
25 i=0;
26
27 while (decimal!=0)
28 {
29 rem=decimal%16;
30 switch(rem)
31 {
32 case 10:
33 hex[i]='A';
34 break;
35
36 case 11:
37 hex[i]='B';
38 break;
39
40 case 12:
41 hex[i]='C';
42 break;
43
44 case 13:
45 hex[i]='D';
46 break;
47
48 case 14:
49 hex[i]='E';
50 break;
51
52 case 15:
53 hex[i]='F';
54 break;
55
56 default:
57 hex[i]=rem+'0'; // to make char to int
58 break;
59 }
60 ++i;
61 decimal/=16;
62 }
63
64 hex[i]='';
65
66 /* reverse the hex string */
67 strrev(hex);
68
69 printf("Hexadecimal number : %s ",hex);
70
71 return 0;
72 }
 
 OUTPUT :

Enter octal number : 232
Hexadecimal number : 9A

Related Post

Leave a Reply

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