mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
Implemented octal to hexadecimal conversion Fixes: #633
This commit is contained in:
parent
d0d67ff789
commit
881e1e9a88
33
conversions/octal_to_hexadecimal.c
Normal file
33
conversions/octal_to_hexadecimal.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Converts octal number to decimal number
|
||||
long octalToDecimal(long octalValue){
|
||||
long decimalValue = 0;
|
||||
int i = 0;
|
||||
while (octalValue) {
|
||||
// Extracts right-most digit, multiplies it with 8^i, and increment i by 1
|
||||
decimalValue += (long)(octalValue % 10) * pow(8, i++);
|
||||
// Shift right in base 10
|
||||
octalValue /= 10;
|
||||
}
|
||||
return decimalValue;
|
||||
}
|
||||
char *octalToHexadecimal(long octalValue){
|
||||
char *hexadecimalValue = malloc(256 * sizeof(char));
|
||||
sprintf(hexadecimalValue, "%lX", octalToDecimal(octalValue));
|
||||
return hexadecimalValue;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int octalValue;
|
||||
|
||||
printf("Enter an octal number: ");
|
||||
scanf("%d", &octalValue);
|
||||
|
||||
//Calling the function octalToHexadecimal
|
||||
char *hexadecimalValue = octalToHexadecimal(octalValue);
|
||||
printf("\nEquivalent hexadecimal number is: %s", hexadecimalValue);
|
||||
free(hexadecimalValue);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user