Saturday, 23 August 2014

C Program to Convert Hexadecimal to Octal and Vice Versa

C Program to Convert Hexadecimal to Octal and Vice Versa


Here a simple C Program to Convert Hexadecimal to Octal and Vice Versa is given, which directly takes octal value from user or display the given number in octal format using %o string format. Similarly, you can directly takes hexadecimal value from user or display the number in hexadecimal format using %x string. This can be demonstrated by an example:


C Program to Convert Hexadecimal to Octal and Vice Versa


#include <stdio.h>
int main()
{
  int n;
  printf("Enter an octal number: ");
  scanf("%o",&n);       /*Takes number in octal format. */

/* %o and %x will display the number is octal format and hexadecimal form respectively. */
  printf("%o in octal = %x in hexadecimal", n, n);

  printf("\nEnter an hexadecimal number: ");
  scanf("%x",&n);       /* Takes number in hexadecimal format.*/
  printf("%x in hexadecimal = %o in octal", n, n);
  return 0;

}


Output
C Program to Convert Hexadecimal to Octal and Vice Versa