请用c++写一段进制转换代码
时间: 2024-04-05 14:09:59 浏览: 195
进制转换的代码
Sure, 进制转换代码如下:
```c
#include <stdio.h>
void convert(int num, int base) {
int rem = num % base;
if (num == 0) {
return;
} else {
convert(num / base, base);
if (rem < 10) {
printf("%d", rem);
} else {
printf("%c", rem - 10 + 'A');
}
}
}
int main() {
int num, base;
printf("Enter a decimal number: ");
scanf("%d", &num);
printf("Enter the base to convert to (2-16): ");
scanf("%d", &base);
if (base < 2 || base > 16) {
printf("Invalid input\n");
} else {
printf("%d base 10 = ", num);
convert(num, base);
printf(" base %d\n", base);
}
return 0;
}
```
如果您有关于代码的任何问题,请随时问我!
阅读全文