实现进制转化
1.C语言实现十进制转二进制、八进制:
include <stdio.h>
include <math.h>
int main(void)
{
int m,i,quotient = 1,remainder,n = 0,answer = 0;
printf("Enter the new base:");
scanf("%d",&m);
printf("Enter the number to be converted:");
scanf("%d",&i);
while (quotient)
{
quotient = m/i;
remainder = m%i;
m = quotient;
answer = remainder*pow(10,n)+answer;
n++;
}
printf("The answer is %d\n",answer);
return 0;
}
3.C语言实现十进制转十六进制:
include<stdlib.h>
include<stdio.h>
void main()
{
char arr[] = "0123456789ABCDEF";
char hex[16];
int i = 0;
int j = 0;
int num = 0, a = 0;
printf("Enter a integer:");
scanf_s("%d", &num);
a = num;
while (num)
{
hex[i++] = arr[num % 16];
num = num / 16;
}
printf("十进制数%d转换成十六进制数是:", a);
for (j = i - 1; j >= 0; --j)
printf("%c", hex[j]);
printf("\n");
}