编程题:十进制转换为八进制
输入一个十进制数: 78 十进制数 78 转换为八进制为 116
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int main(void) { int n[50], t = -1, sum = 0; int num, num1;
puts("请输入一个十进制数"); scanf("%d", &num); num1 = num; do { t++; n[t] = num % 8; printf("Step %d %d/2,余数=%d,商=%d: \n", t + 1, num, n[t], num / 2); num /= 8; } while (num > 0);//商大于0 printf("%d的八进制为:\n", num1); for (int i = t; i >= 0; i--) {//-- printf("%d", n[i]); } return 0; }
#include<iostream>
using namespace std;
long long transformFromDecimalToOctal(long long decimal)
{
if(decimal==0)
return 0;
}
long long temporary=decimal%8;
return temporary+transformFromDecimalToOctal(decimal/8)*10;
int main()
long long decimal;
cout<<"DecimalNumber"<<endl;
cin>>decimal;
cout<<"OctalNumber"<<endl;
cout<<transformFromDecimalToOctal(decimal)<<endl;
#include <stdi...
用户登录可进行刷题及查看答案
#include <stdio.h> #include <math.h> int convertDecimalToOctal(int decimalNumber); int main() { int decimalNumber; printf("输入一个十进制数: "); scanf("%d", &decimalNumber); printf("十进制数 %d 转换为八进制为 %d", decimalNumber, convertDecimalToOctal(decimalNumber)); return 0; } int convertDecimalToOctal(int decimalNumber) { int octalNumber = 0, i = 1; while (decimalNumber != 0) { octalNumber += (decimalNumber % 8) * i; decimalNumber /= 8; i *= 10; } return octalNumber; }
登录后提交答案