Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 113f92d

Browse files
ADD Integer-To-English-Words problem #79
1 parent 5b34441 commit 113f92d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

‎Hard/integer-to-english-words.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
public class Solution
2+
{
3+
private static bool _IsRemainderZero(int num, int divisionBy)
4+
=> (num % divisionBy) == 0;
5+
6+
public static string NumberToWords(int num)
7+
{
8+
if (num == 0)
9+
{
10+
return "Zero";
11+
}
12+
13+
if (num >= 1 && num <= 19)
14+
{
15+
string[] arr = { "", "One", "Two", "Three", "Four", "Five",
16+
"Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
17+
"Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
18+
"Eighteen", "Nineteen" };
19+
20+
return arr[num];
21+
}
22+
23+
if (num >= 20 && num <= 99)
24+
{
25+
string[] arr = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
26+
"Seventy", "Eighty", "Ninety" };
27+
28+
return $"{arr[num / 10]} {(_IsRemainderZero(num, 10) ? string.Empty : NumberToWords(num % 10))}".Trim();
29+
}
30+
31+
if (num >= 100 && num <= 999)
32+
{
33+
return $"{NumberToWords(num / 100)} Hundred {(_IsRemainderZero(num, 100) ? string.Empty : NumberToWords(num % 100))}".Trim();
34+
}
35+
36+
if (num >= 1000 && num <= 999999)
37+
{
38+
return $"{NumberToWords(num / 1000)} Thousand {(_IsRemainderZero(num, 1000) ? string.Empty : NumberToWords(num % 1000))}".Trim();
39+
}
40+
41+
if (num >= 1000000 && num <= 999999999)
42+
{
43+
return $"{NumberToWords(num / 1000000)} Million {(_IsRemainderZero(num, 1000000) ? string.Empty : NumberToWords(num % 1000000))}".Trim();
44+
}
45+
46+
if (num >= 1000000000)
47+
{
48+
return $"{NumberToWords(num / 1000000000)} Billion {(_IsRemainderZero(num, 1000000000) ? string.Empty : NumberToWords(num % 1000000000))}".Trim();
49+
}
50+
51+
return string.Empty;
52+
}
53+
54+
static void Main()
55+
{
56+
Console.WriteLine(NumberToWords(110));
57+
Console.ReadKey();
58+
}
59+
}
60+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /