Numbers To Roman

This algorithm converts decimal numbers to roman numerals.



									/*****Please include following header files*****/
// string
/***********************************************/

/*****Please use following namespaces*****/
// std
/*****************************************/

static string RepeatString(string s, int count) {
	if (count < 0) return "";

	string str = "";

	for (int i = 0; i < count; ++i)
	{
		str += s;
	}

	return str;
}

static string NumbersToRoman(int num) {
	string roman = "";

	int totalM = num / 1000;
	num %= 1000;
	int totalCM = num / 900;
	num %= 900;

	int totalD = num / 500;
	num %= 500;
	int totalCD = num / 400;
	num %= 400;

	int totalC = num / 100;
	num %= 100;
	int totalXC = num / 90;
	num %= 90;

	int totalL = num / 50;
	num %= 50;
	int totalXL = num / 40;
	num %= 40;

	int totalX = num / 10;
	num %= 10;
	int totalIX = num / 9;
	num %= 9;

	int totalV = num / 5;
	num %= 5;
	int totalIV = num / 4;
	num %= 4;

	roman += RepeatString("M", totalM);
	roman += RepeatString("CM", totalCM);
	roman += RepeatString("D", totalD);
	roman += RepeatString("CD", totalCD);
	roman += RepeatString("C", totalC);
	roman += RepeatString("XC", totalXC);
	roman += RepeatString("L", totalL);
	roman += RepeatString("XL", totalXL);
	roman += RepeatString("X", totalX);
	roman += RepeatString("IX", totalIX);
	roman += RepeatString("V", totalV);
	roman += RepeatString("IV", totalIV);
	roman += RepeatString("I", num);

	return roman;
}
								


Example

									int data = 419;
string value = NumbersToRoman(data);
								


Output

									CDXIX