C语言程序怎么把十进制的数转换成八进制的数?

2024-11-15 07:39:21
推荐回答(2个)
回答1:

C语言程序十进制的数转换成八进制的数的办法:

#include
#include
void main()
{
int n,a,sum = 0,i =0;
printf("十进制输出一个数n\n");
scanf("%d",&n);
while(n)
{
a = n%8;
n = n/8;
sum += a*pow(10,i);
i++;
}
printf("八进制输出sum:%d",sum);
}

回答2:

#include
#include
void main()
{
int n,a,sum = 0,i =0;
printf("十进制输出一个数n\n");
scanf("%d",&n);
while(n)
{
a = n%8;
n = n/8;
sum += a*pow(10,i);
i++;
}
printf("八进制输出sum:%d",sum);
}