这是一个编程题模板。
编程实现将任意的十进制整数N转换成R进制数(R在2-16之间)。N<0则直接退出。
输入格式:
一行中输入两个数分别是N与R,两者之间以空格隔开。
输出格式:
将转换后的R进行数。
这是一个编程题模板。
编程实现将任意的十进制整数N转换成R进制数(R在2-16之间)。N<0则直接退出。
输入格式:
一行中输入两个数分别是N与R,两者之间以空格隔开。
输出格式:
将转换后的R进行数。
关注【以下回答由 GPT 生成】
```c
#include <stdio.h>
// Function to convert decimal to any base
void decimalToAnyBase(int N, int R) {
if (N < 0) {
printf("N should be a positive number");
return;
}
// Array to store the digits of the result
char result[100];
int i = 0;
// Convert the number from decimal to base R
while (N > 0) {
int remainder = N % R;
if (remainder < 10) {
result[i] = remainder + '0';
} else {
result[i] = remainder - 10 + 'A';
}
N = N / R;
i++;
}
// Print the result in reverse order
printf("The converted number is: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", result[j]);
}
}
int main() {
int N, R;
printf("Enter N and R: ");
scanf("%d %d", &N, &R);
decimalToAnyBase(N, R);
return 0;
}
```
【相关推荐】