各个进制之间转换的C语言程序
时间: 2024-05-16 21:14:42 浏览: 14
以下是一个将十进制数转换为任意进制数的C语言程序:
```c
#include <stdio.h>
void convert(int num, int base);
int main() {
int num, base;
printf("Enter the number to be converted: ");
scanf("%d", &num);
printf("Enter the base of the desired output: ");
scanf("%d", &base);
printf("The converted number is: ");
convert(num, base);
printf("\n");
return 0;
}
void convert(int num, int base) {
if (num == 0) {
return;
} else {
convert(num / base, base);
int remainder = num % base;
if (remainder < 10) {
printf("%d", remainder);
} else {
printf("%c", remainder - 10 + 'A');
}
}
}
```
该程序使用递归方法将十进制数转换为任意进制数。在`convert`函数中,当`num`为0时,递归结束;否则,函数将`num`除以`base`并递归地调用自身,然后打印出余数。如果余数小于10,则打印出数字;否则,打印出相应的字母。
相关推荐
![](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)
![](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)