Numbers To Roman

This algorithm converts decimal numbers to roman numerals.



									char* AppendString(const char* str1, const char* str2) {
	int str1Len = strlen(str1);
	int str2Len = strlen(str2);
	int strLen = str1Len + str2Len + 1;
	char* str = malloc(strLen);

	for (int i = 0; i < str1Len; i++)
		str[i] = str1[i];

	for (int i = 0; i < str2Len; i++)
		str[(str1Len + i)] = str2[i];

	str[strLen - 1] = '\0';

	return str;
}

char* RepeatString(char* s, int count)
{
	if (count < 0) return "";

	char* str = "";

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

	return str;
}

char* NumbersToRoman(int num) {
	char* 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 = AppendString(roman, RepeatString("M", totalM));
	roman = AppendString(roman, RepeatString("CM", totalCM));
	roman = AppendString(roman, RepeatString("D", totalD));
	roman = AppendString(roman, RepeatString("CD", totalCD));
	roman = AppendString(roman, RepeatString("C", totalC));
	roman = AppendString(roman, RepeatString("XC", totalXC));
	roman = AppendString(roman, RepeatString("L", totalL));
	roman = AppendString(roman, RepeatString("XL", totalXL));
	roman = AppendString(roman, RepeatString("X", totalX));
	roman = AppendString(roman, RepeatString("IX", totalIX));
	roman = AppendString(roman, RepeatString("V", totalV));
	roman = AppendString(roman, RepeatString("IV", totalIV));
	roman = AppendString(roman, RepeatString("I", num));

	return roman;
}
								


Example

									int data = 419;
char* value = NumbersToRoman(data);
								


Output

									CDXIX