Posts Tagged ‘roman numeral’

Decimal to Roman Numeral Conversion

21. May 2009

No Comments »

[code lang="cpp"]#include
#include
using namespace std;

string dec_to_numeral(int x) {
int dec[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string num[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string numeral;

for(int i = 0; i < 13; i++) {
while (x >= dec[i]) {
x -= dec[i];
numeral.append(num[i]);
}
}

return numeral;
}

int main() {
//example
cout << dec_to_numeral(400);

return 0;
}[/code]