Decimal to Roman Numeral Conversion

[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]

Related posts:

  1. Binary to Decimal Conversion Converts a binary number to its decimal equivalent. However unlike the bindec function, this will preserve the binary/radix point. Code...
  2. Monte Carlo Generation of Pi [code lang="cpp"]#include #include #include #include int main () { int in = 0; srand(time(NULL)); for(int i = 0; i <...
  3. Euler’s Constant Calculate Euler’s constant. [code lang="cpp"]#include /** * This code will calculate e * as the sum of the infinite series...

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.