include

3
#include <stdio.h> #include <math.h> #include <string.h> void octal_hex(int n, char hex[]); int hex_octal(char hex[]); int main() { char hex[20],c; int n; printf("Instructions:\n"); printf("Enter h to convert octal to hexadecimal:\n"); printf("Enter o to hexadecimal number to octal:\n"); printf("Enter a character: "); scanf("%c",&c); if (c=='h' || c=='H') { printf("Enter octal number: "); scanf("%d",&n); octal_hex(n,hex); printf("Hexadecimal number: %s",hex); } if (c=='o' || c=='O') { printf("Enter hexadecimal number: "); scanf("%s",hex); printf("Octal number: %d",hex_octal(hex)); } return 0; } void octal_hex(int n, char hex[]) /* Function to convert octal to hexadecimal. */ { int i=0,decimal=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(8,i); ++i; } /* At this point, decimal contains the decimal value of given octal number. */ i=0; while (decimal!=0) { rem=decimal%16; switch(rem) { case 10: hex[i]='A';

Upload: ishan-raina

Post on 30-Sep-2015

212 views

Category:

Documents


0 download

DESCRIPTION

g

TRANSCRIPT

#include #include #include void octal_hex(int n, char hex[]);int hex_octal(char hex[]);int main(){ char hex[20],c; int n; printf("Instructions:\n"); printf("Enter h to convert octal to hexadecimal:\n"); printf("Enter o to hexadecimal number to octal:\n"); printf("Enter a character: "); scanf("%c",&c); if (c=='h' || c=='H') { printf("Enter octal number: "); scanf("%d",&n); octal_hex(n,hex); printf("Hexadecimal number: %s",hex); } if (c=='o' || c=='O') { printf("Enter hexadecimal number: "); scanf("%s",hex); printf("Octal number: %d",hex_octal(hex)); } return 0;}

void octal_hex(int n, char hex[]) /* Function to convert octal to hexadecimal. */{ int i=0,decimal=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(8,i); ++i; }/* At this point, decimal contains the decimal value of given octal number. */ i=0; while (decimal!=0) { rem=decimal%16; switch(rem) { case 10: hex[i]='A'; break; case 11: hex[i]='B'; break; case 12: hex[i]='C'; break; case 13: hex[i]='D'; break; case 14: hex[i]='E'; break; case 15: hex[i]='F'; break; default: hex[i]=rem+'0'; break; } ++i; decimal/=16; } hex[i]='\0'; strrev(hex); /* Function to reverse string. */}

int hex_octal(char hex[]) /* Function to convert hexadecimal to octal. */{ int i, length, decimal=0, octal=0; for(length=0; hex[length]!='\0'; ++length); for(i=0; hex[i]!='\0'; ++i, --length) { if(hex[i]>='0' && hex[i]='A' && hex[i]='a' && hex[i]