请写一个吧十进制转化为十六进制的C语言代码
时间: 2024-03-05 18:48:43 浏览: 67
十进制转换为二进制_技术要点-client-side.data.storage.keeping.it.local.14919
以下是将十进制数转换为十六进制数的C语言代码:
```c
#include <stdio.h>
void decimal_to_hexadecimal(int decimal_num) {
int remainder, quotient;
int i = 1, j, temp;
char hexadecimal_num[100];
quotient = decimal_num;
while (quotient != 0) {
temp = quotient % 16;
// To convert integer into character
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
hexadecimal_num[i++] = temp;
quotient = quotient / 16;
}
printf("Hexadecimal number of %d is: ", decimal_num);
for (j = i - 1; j > 0; j--)
printf("%c", hexadecimal_num[j]);
}
int main() {
int decimal_num;
printf("Enter a decimal number: ");
scanf("%d", &decimal_num);
decimal_to_hexadecimal(decimal_num);
return 0;
}
```
该程序接受一个十进制数作为输入,并将其转换为十六进制数,然后将其打印在屏幕上。
阅读全文