You are on page 1of 2

/* ROLL NO: 1 SEM-1 ICA DIV: A

NAME : VARUN AGGARWAL SUBJECT: FOP


PROGRAM DEFINITION: Write a program to convert hexadecimal value to octal
value
*/

#include <stdio.h>
#include <math.h>
#include <conio.h>
#define size 3

char ch[size];
int htoo();

int main()
{
int n,oct;
clrscr();
printf("\n Enter Hexadecimal no. ::");
scanf("%s",ch);
oct=htoo();
printf("\nOctal value is %d",oct);
getch();
return 0;
}

int htoo()
{
int x,i=0,dec=0,c=0,temp, n=0,temp1,oct=0;;
for(i=strlen(ch)-1;i>=0;i--)
{
switch(ch[i])
{
case 'A':
case 'a':
dec+=10*pow(16,c);
break;
case 'B' :
case 'b':
dec+=11*pow(16,c);
break;
case 'C' :
case 'c':
dec+=12*pow(16,c);
break;
case 'D' :
case 'd':
dec+=13*pow(16,c);
break;
case 'E' :
case 'e':
dec+=14*pow(16,c);
break;
case 'F' :
case 'f':
dec+=15*pow(16,c);
break;
case '1':
dec+=1*pow(16,c);
break;
case '2':
dec+=2*pow(16,c);
break;
case '3':
dec+=3*pow(16,c);
break;
case '4':
dec+=4*pow(16,c);
break;
case '5':
dec+=5*pow(16,c);
break;
case '6':
dec+=6*pow(16,c);
break;
case '7':
dec+=7*pow(16,c);
break;
case '8':
dec+=8*pow(16,c);
break;
case '9':
dec+=9*pow(16,c);
break;
}
c++;
}
printf("\nDecimal value is %d",dec);
while(dec>0)
{
temp1=dec%8;
oct+=temp1*pow(10,n);
n++;
dec=dec/8;
}
return oct;
}
/* OUTPUT:
Enter Hexadecimal no. ::1A5
Decimal value is 421
Octal value is 645
*/