Here is a very simple example of an algorithm to convert the number 115 to a string:
let a = 115
let i = 0
allocate memory for string c
loop until a = 0
c[i] = a % 10
a = a / 10
i = i + 1
end loop
reverse string c
Good job (looks fine) but I wonder how the above will produce desired result : one hundred and fifteen.
I don't like doing people's homework for them
That is 4 people like u who still has homeworks. I left university more than 7 years ago (i am trying to figure out where u were then)
I deliberately did not post code for this. However, there should be enough info in both the example above for you to figure it out.
Your thread reads
Re: Post Your "c" Program Questions Here
Sorry man for disappointing u but i do post codes. Here is the code that converts amount in double to amount in words. I did it in C#. You may copy and use it if u like. It uses Nigeria currency if u like:
public static string AmountInWord(string amount)
{
long num;
long num2;
string str2;
string str3;
string str = "";
string[] strArray = new string[5];
amount = amount.Replace(",", "");
amount = amount.Replace(" ", "");
str = "";
try
{
double num3 = double.Parse(amount);
}
catch
{
throw new Exception("The amount supplied is not a number and therefore not convertuble");
}
strArray[0] = "Trillion ";
strArray[1] = "Billion ";
strArray[2] = "Million ";
strArray[3] = "Thousand ";
strArray[4] = " ";
int index = amount.IndexOf(".");
if (index > -1)
{
str2 = amount.Substring(0, index);
str3 = amount.Substring(index, amount.Length - index);
if (str2.Length > 15)
{
throw new Exception("The amount supplied must not be greater than 999 999 999 999 999.99");
}
str2 = string.Format("{0:D15}", long.Parse(str2));
str3 = string.Format("{0:F2}", double.Parse(str3));
}
else
{
if (amount.Length > 15)
{
throw new Exception("The amount supplied must not be greater than 999 999 999 999 999.99");
}
str2 = string.Format("{0:D15}", long.Parse(amount));
str3 = "0.00";
}
amount = str2 + str3.Substring(1, 3);
int num5 = 0;
for (int i = 0; num5 <= 4; i += 3)
{
if (long.Parse(amount.Substring(i, 3)) > 0)
{
if (long.Parse(amount.Substring(i, 1)) > 0)
{
num = long.Parse(amount.Substring(i + 1, 1));
num2 = long.Parse(amount.Substring(i + 2, 1));
if ((num > 0) || (num2 > 0))
{
str = str + convert(amount.Substring(i, 1)) + "Hundred And ";
}
else
{
str = str + convert(amount.Substring(i, 1)) + "Hundred ";
}
}
num = long.Parse(amount.Substring(i + 1, 2));
num2 = long.Parse(amount.Substring(i + 1, 2));
if ((num >= 20) && (num2 <= 0x63))
{
num = long.Parse(amount.Substring(i + 1, 1) + "0");
num2 = long.Parse(amount.Substring((i + 1) + 1, 1));
str = str + convert(num) + convert(num2);
}
num = long.Parse(amount.Substring(i + 1, 2));
num2 = long.Parse(amount.Substring(i + 1, 2));
if ((num >= 1) && (num2 < 20))
{
str = str + convert(amount.Substring(i + 1, 2));
}
str = str + strArray[num5];
}
num5++;
}
if (long.Parse(amount.Substring(0, 15)) > 0)
{
str = str + "naira ";
}
num = int.Parse(amount.Substring(amount.Length - 2, 2));
if (num > 0)
{
num = long.Parse(amount.Substring(amount.Length - 2, 2));
num2 = long.Parse(amount.Substring(amount.Length - 2, 2));
if ((num >= 20) && (num2 <= 0x63))
{
num = long.Parse(amount.Substring(0x10, 1) + "0");
num2 = long.Parse(amount.Substring(0x11, 1));
str = str + convert(num) + convert(num2);
}
num = long.Parse(amount.Substring(amount.Length - 2, 2));
if ((num >= 1) && (num < 20))
{
str = str + convert(amount.Substring(amount.Length - 2, 2));
}
str = str + "kobo ";
}
return (str + "only").ToLower();
}
private static string convert(long amount)
{
return convert(amount.ToString());
}
private static string convert(string amount)
{
switch (int.Parse(amount))
{
case 70:
return "seventy ";
case 80:
return "eighty ";
case 90:
return "ninety ";
case 0:
return "";
case 1:
return "one ";
case 2:
return "two ";
case 3:
return "three ";
case 4:
return "four ";
case 5:
return "five ";
case 6:
return "six ";
case 7:
return "seven ";
case 8:
return "eight ";
case 9:
return "nine ";
case 10:
return "ten ";
case 11:
return "eleven ";
case 12:
return "twelve ";
case 13:
return "thirteen ";
case 14:
return "fouteen ";
case 15:
return "fifteen ";
case 0x10:
return "sixteen ";
case 0x11:
return "seventeen ";
case 0x12:
return "eighteen ";
case 0x13:
return "nineteen ";
case 20:
return "twenty ";
case 30:
return "thirty ";
case 40:
return "fourty ";
case 50:
return "fifty ";
case 60:
return "sixty ";
}
return "";
}
public static bool IsNumeric(string val)
{
try
{
double num = double.Parse(val);
return true;
}
catch
{
return false;
}
}
Good luck