求一个c++程序代码?

来源:百度知道 编辑:UC知道 时间:2024/06/24 10:04:36
编写一个函数,输入一个十进制数,输出一个十六进制数.

#include<iostream>
using namespace std;

int main()
{
int decimal, n=16;
cout<<"请输入十进制数:";
cin>>decimal;

int quotient, remainder[10]; //商,余数数组

for(int i=0; i<10; i++)
{
quotient=decimal/n;
remainder[i]=decimal%n;
decimal=quotient;

if(quotient==0) break;
}

for(; i>=0; i--)
{
if(remainder[i]==10) { cout<<"A"; }
else if(remainder[i]==11) { cout<<"B"; }
else if(remainder[i]==12) { cout<<"C"; }
else if(remainder[i]==13) { cout<<"D"; }
else if(remainder[i]==14) { cout<<"E"; }
else if(remainder[i]==15) { cout<<"F"; }
else cout<<remainder[i];
}
cout<<endl;
return 0;
}

//---------------------------------------------------------------------------