In this tutorial you will learn about the C Program to Convert Octal to Hexadecimal and its application with practical example.
C Program to Convert Octal to Hexadecimal
In this tutorial, we will learn to create a C program that will Convert Octal to Hexadecimal in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
Program to Convert Octal to Hexadecimal:-
As we all know the c is a very powerful language. With the help of c programming language, we can make many programs. We cal perform many input-output operations using c programming. In today’s tutorial, we take the input in octal from the user and convert it into hexadecimal. With the help of c programming, we can perform many conversion operations.
With the help of this program, we can Convert Octal to Hexadecimal.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Takeing the input number from from the user in octal for the program. 3. Passing that input to the string function. 4. Pass that number to a for loop for convertion. 4. Print the Result. 5. End the program. |
Program to Convert Octal to Hexadecimal:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#include <stdio.h> #include<string.h> int main() { //declaring the variable for the program int octaltobinary[]={0,1,10,11,100,101,110,111}; char hexadecimal[10]; char hex[10]; long int binary=0; int octal; int rem=0; int position=1; int len=0; int k=0; //taking input in the octal number printf("Enter a octal number"); scanf("%d",&octal); // Converting octal number into a binary number. while(octal!=0) { rem=octal%10; binary=octaltobinary[rem]*position+binary; octal=octal/10; position=position*1000; } printf("The binary number is : %ld",binary); // Converting binary number into a hexadecimal number. while(binary > 0) { rem = binary % 10000; switch(rem) { case 0: strcat(hexadecimal, "0"); break; case 1: strcat(hexadecimal, "1"); break; case 10: strcat(hexadecimal, "2"); break; case 11: strcat(hexadecimal, "3"); break; case 100: strcat(hexadecimal, "4"); break; case 101: strcat(hexadecimal, "5"); break; case 110: strcat(hexadecimal, "6"); break; case 111: strcat(hexadecimal, "7"); break; case 1000: strcat(hexadecimal, "8"); break; case 1001: strcat(hexadecimal, "9"); break; case 1010: strcat(hexadecimal, "A"); break; case 1011: strcat(hexadecimal, "B"); break; case 1100: strcat(hexadecimal, "C"); break; case 1101: strcat(hexadecimal, "D"); break; case 1110: strcat(hexadecimal, "E"); break; case 1111: strcat(hexadecimal, "F"); break; } len=len+1; binary /= 10000; } for(int i=len-1;i>=0;i--) { hex[k]=hexadecimal[i]; k++; } hex[len]='\0'; //printing the output in the hexadecimal printf("\nThe hexadecimal number is :"); for(int i=0; hex[i]!='\0';i++) { printf("%c",hex[i]); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- hexadecimal[] = it will hold the character value for the program.
- octaltobinary[] = in will hold the integer value.
- i = it will hold the integer value.
Taking the input.