用c语言二维数组写一个十进制转十六进制的代码
时间: 2023-11-17 17:07:40 浏览: 33
以下是一个用C语言二维数组写的十进制转十六进制的代码:
#include <stdio.h>
int main() {
int decimal, quotient, remainder;
char hex[100];
int i, j = 0;
printf("请输入一个十进制数:");
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("转换成十六进制为:");
for (i = j - 1; i >= 0; i--)
printf("%c", hex[i]);
return 0;
}
该程序首先从用户输入一个十进制数,然后使用while循环将其转换为十六进制,并将结果存储在一个字符数组中。最后,使用for循环将该字符数组中的字符输出到屏幕上,以显示十六进制值。
相关问题
用c语言写一个十进制转十六进制的代码
#include<stdio.h>
int main(){
int n, i=0;
char hex[100];
printf("请输入一个十进制数:");
scanf("%d",&n);
while(n!=0){
int temp = n%16;
if(temp<10){
hex[i] = temp + 48;
}
else{
hex[i] = temp + 55;
}
n = n/16;
i++;
}
printf("转换为十六进制为:");
for(int j=i-1;j>=0;j--){
printf("%c",hex[j]);
}
printf("\n");
return 0;
}
写一个十进制转十六进制的C语言代码
以下是将十进制数转换为十六进制的C语言代码:
```c
#include <stdio.h>
int main()
{
int decimal, quotient, remainder;
int i = 1, j, temp;
char hexadecimal[100];
printf("Enter a decimal number: ");
scanf("%d", &decimal);
quotient = decimal;
while (quotient != 0)
{
temp = quotient % 16;
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
hexadecimal[i++] = temp;
quotient = quotient / 16;
}
printf("Hexadecimal equivalent is: ");
for (j = i - 1; j > 0; j--)
printf("%c", hexadecimal[j]);
return 0;
}
```
这个程序的基本思路是:
1. 首先,从用户接收一个十进制数。
2. 然后,将该数字除以16,得到商和余数。
3. 如果余数小于10,则将其转换为字符并将其添加到一个数组中。
4. 如果余数大于等于10,则将其转换为相应的字母(A、B、C、D、E或F)并将其添加到数组中。
5. 重复第2步,直到商为0。
6. 最后,从数组的末尾开始,将所有字符打印出来,以获得十六进制等效项。