编写一段c语言将任意10进制数转换为16进制
时间: 2023-07-26 07:14:04 浏览: 117
好的,以下是将十进制数转换为十六进制的C语言代码:
```c
#include <stdio.h>
void decimalToHexadecimal(int decimal) {
int remainder, quotient;
int i, j = 0;
char hexadecimal[100];
quotient = decimal;
while (quotient != 0) {
remainder = quotient % 16;
if (remainder < 10)
hexadecimal[j++] = 48 + remainder;
else
hexadecimal[j++] = 55 + remainder;
quotient = quotient / 16;
}
printf("The hexadecimal value of %d is: ", decimal);
for (i = j; i >= 0; i--)
printf("%c", hexadecimal[i]);
}
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
decimalToHexadecimal(decimal);
return 0;
}
```
该程序将输入的十进制数转换为十六进制,并输出结果。
阅读全文