我想要一个十进制转十六进制的c程序
时间: 2023-10-19 14:09:54 浏览: 38
下面是一个将十进制数转换为十六进制数的C程序:
```
#include <stdio.h>
int main() {
int decimal, quotient, remainder;
int i, j = 0;
char hex[100];
printf("Enter a decimal number: ");
scanf("%d", &decimal);
quotient = decimal;
while (quotient != 0) {
remainder = quotient % 16;
if (remainder < 10)
hex[j++] = remainder + 48;
else
hex[j++] = remainder + 55;
quotient /= 16;
}
printf("Hexadecimal equivalent of %d is: ", decimal);
for (i = j - 1; i >= 0; i--)
printf("%c", hex[i]);
return 0;
}
```
该程序首先要求用户输入一个十进制数。然后,它将该数除以16,直到商为0,每次取余数并将其转换为十六进制数字。如果余数小于10,则将其转换为字符,添加到字符数组中。如果余数大于或等于10,则将其转换为相应的十六进制字符(A-F),并将其添加到字符数组中。最后,程序按相反的顺序打印字符数组中的字符,以显示十六进制等效项。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)