Tuesday, February 10, 2015

Hexadecimal to Octal

/* Convert Hexadecimal to Octal */ #include <math.h> #include <stdio.h> int main() { printf("Enter the hexadecimal number: "); char hexNum[100]; scanf("%s", &hexNum); int i = 0; while (hexNum[i] != '\0') { i++; } int strlen = i; int decimal = 0; for (i=0; i<strlen; i++) { int e = strlen - i - 1; int n = 0; switch(hexNum[i]) { case '0': n = 0; break; case '1': n = 1; break; case '2': n = 2; break; case '3': n = 3; break; case '4': n = 4; break; case '5': n = 5; break; case '6': n = 6; break; case '7': n = 7; break; case '8': n = 8; break; case '9': n = 9; break; case 'A': n = 10; break; case 'B': n = 11; break; case 'C': n = 12; break; case 'D': n = 13; break; case 'E': n = 14; break; case 'F': n = 15; break; } decimal = decimal + (n * pow(16, e)); } int quotient = decimal; int octalNumber[100]; int j = 0; i = 1; while(quotient!=0) { octalNumber[i++] = quotient % 8; quotient = quotient / 8; } printf("Equivalent octal value: "); for(j = i - 1 ;j> 0;j--) printf("%d", octalNumber[j]); getch(); return 0; }

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2015 Code Fervor™ is a registered trademark.

Designed by Templateism By Naman Kumar