Simple Calendar with option for different months - c#

Here's the prompt: Write a program
that prints a calendar for one month. Input
consists of an integer specifying the first
day of the month (1 = Sunday) and an
integer specifying how many days are
in a month.
Here is a sample output of what it should look like:
First day of the month 3
Number of days in the month 31
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
I've been working on it and here is what I have so far.
namespace Program_202t
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Program 202t";
Console.Write("Enter the first day of the month: ");
int year = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of days in a month: ");
int month = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nSunday Monday Tuesday Wedsnesday Thursday Friday Saturday");
if (year == 1)
{
Console.SetCursorPosition(0, 4);
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
if (year == 2)
{
//Console.SetCursorPosition(10, 4);
for (int i = 1; i <= month; i++)
{
if (i == 1)
{
Console.Write(" " + i + " ");
}
else if (i > 1 && i < 11 && i != 6)
{
Console.Write(i + " ");
}
Console.Write("\b");
if (i == 6)
{
Console.Write(i + "\n");
}
if (i > 11 && i != 14)
{
Console.Write(i + " ");
}
if (i == 14)
{
Console.Write(i + "\n");
}
/*if (((i) % 6) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
//FIX!!!
for (int b = 11; b <= month; b++)
{
if (((b) % 13) > 0 && b >13)
{
Console.Write(b + " ");
}
if (((b) % 20) > 0 && b <= 13)
{
Console.Write(b + " ");
}
else
{
Console.Write("\n");
}
}*/
}
if (year == 3)
{
Console.SetCursorPosition(20, 4);
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 5) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write("\n");
}
}
}
if (year == 4)
{
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
if (year == 5)
{
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
if (year == 6)
{
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
if (year == 7)
{
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
if (((i) % 7) > 0)
{
}
else
{
Console.Write("\n");
}
}
Console.Write("\b");
for (int b = 11; b <= month; b++)
{
if (((b) % 7) > 0)
{
Console.Write(b + " ");
}
else
{
Console.Write(b + "\n");
}
}
}
Console.ReadLine();
}
}
}
Do any of you know what I am doing wrong for the years that aren't 1? One works fine, but the rest have various problems.
Thanks for the help!

Will this do?
class Program
{
static void Main(string[] args)
{
Console.Title = "Program 202t";
Console.Write("Enter the first day of the month: ");
int startingDay = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of days in a month: ");
int daysInMonth = Convert.ToInt32(Console.ReadLine());
List<string> daysOfTheWeek = new List<string>() {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
foreach (string day in daysOfTheWeek)
{
Console.Write($"{day,10}");
}
List<string> days = new List<string>();
for (int i = 0; i < startingDay; i++)
{
days.Add($"{"",10}");
}
for (int i = 1; i < daysInMonth+1; i++)
{
days.Add($"{i,10}");
}
for (int i = 0; i < days.Count; i++)
{
if (i%7!=0) {Console.Write(days[i]);}
else {Console.WriteLine(days[i]);}
}
}

Alrighty, i did a quick run through of the code and I see where you're issues are so I'll give you some tips on where to look.
For one, your code has logical issues revolving around the required formatting of the output. There are simpler ways to output the correct formatting. If you look at the .NET String Reference then look at formatting, you may find an easier way to do it.
Next, you have an issue with misaligned block endings so some of your code is never getting executed. I recommend you breakpoint your code at the first if statement and step through the code to understand the execution in runtime.
Lastly, I recommend clarifying your variable names a little bit. If the first input represent that day of the week, then perhaps use a variable name that represents it (for example dayOfWeek, but month doesn't do it for me). Also the number of days in the month could use a better variable name.
Please do ask questions if you need some more guidance. I'd be happy to help.

Related

How to make a game move continuously

I'm trying to program a game called NIM (https://plus.maths.org/content/play-win-nim). I've got halfway through, however, when a player makes a move, the board will simply reset to normal for the next move. Any thoughts on how I can make another move to the board and it takes into account the previous move?
static string underline = "\x1B[4m";
static string reset = "\x1B[0m";
static string firstMove;
static bool gameStatus = true;
static void Main(string[] args)
{
Introduction();
InitialBoardSetUp();
PlayingGame();
}
static void Introduction()
{
Console.WriteLine("\t\t\t\t\t" + underline + "Welcome to NIM!\n"+ reset);
Thread.Sleep(1000);
Console.WriteLine(underline + "The rules are as follows:\n" + reset);
Thread.Sleep(1000);
Console.WriteLine(" - Each player takes their turn to remove a certain number of 'blocks' from a stack, of which there are 7.");
Console.WriteLine(" - This happens until there is only 1 'block' remaining. With the winner being the one to remove the last 'block'.\n");
Thread.Sleep(1500);
}
static void InitialBoardSetUp()
{
Console.WriteLine(underline + "This is how the board is formatted:\n" + reset);
Thread.Sleep(750);
Console.Write(" ");
for (int i = 1; i <= 7; i++)
{
Console.Write(" " + i + " ");
}
Console.Write("\n\n");
for (int i = 1; i <= 7; i++)
{
Console.Write(" " + i + " ");
for (int j = 1; j <= 7; j++)
{
Console.Write("███ ");
}
Console.Write("\n");
}
Console.Write("\n\n");
Thread.Sleep(1000);
}
static void WhoGoesFirst()
{
string[] PossibleChoices = { "Computer", "You" };
Random WhoGoesFirst = new Random();
int WGFIndex = WhoGoesFirst.Next(0, 2);
firstMove = PossibleChoices[WGFIndex];
Console.WriteLine("Randomly selecting who goes first...");
Thread.Sleep(1000);
Console.WriteLine("{0} will go first!\n", firstMove);
Thread.Sleep(1000);
}
static void ComputerMove()
{
Random CompStack = new Random();
int CompStackSelection = CompStack.Next(1, 8);
Random CompRemoved = new Random();
int CompRemovedSelection = CompRemoved.Next(1, 8);
Console.WriteLine("Computer is making its move... ");
Thread.Sleep(1000);
Console.WriteLine("Computer has decided to remove {0} blocks from stack number {1}.\n", CompRemovedSelection, CompStackSelection);
Thread.Sleep(1000);
Console.Write(" ");
for (int i = 1; i <= 7; i++)
{
Console.Write(" " + i + " ");
}
Console.Write("\n\n");
for (int i = 1; i <= 7; i++)
{
Console.Write(" " + i + " ");
for (int j = 1; j <= 7; j++)
{
if (j == CompStackSelection && i <= CompRemovedSelection)
{
Console.Write(" ");
}
else
{
Console.Write("███ ");
}
}
Console.Write("\n");
}
Console.Write("\n\n");
Thread.Sleep(1000);
}
static void PlayerMove()
{
Console.Write("Which stack do you wish to remove from?: ");
int PlayerStackSelection = Convert.ToInt32(Console.ReadLine());
// Exception Handling - Can't be greater than 7 or less than 1.
Console.Write("How many blocks do you wish to remove?: ");
int PlayerRemovedSelection = Convert.ToInt32(Console.ReadLine());
// Exception Handling - Can't be greater than 7 or less than 1.
Console.WriteLine();
Console.Write(" ");
for (int i = 1; i <= 7; i++)
{
Console.Write(" " + i + " ");
}
Console.Write("\n\n");
for (int i = 1; i <= 7; i++)
{
Console.Write(" " + i + " ");
for (int j = 1; j <= 7; j++)
{
if (j == PlayerStackSelection && i <= PlayerRemovedSelection)
{
Console.Write(" ");
}
else
{
Console.Write("███ ");
}
}
Console.Write("\n");
}
Console.Write("\n\n");
Thread.Sleep(1000);
}
static void PlayingGame()
{
int gameNumber = 1;
while (gameStatus == true)
{
Console.WriteLine(underline + "Round " + gameNumber+ ":\n" + reset);
WhoGoesFirst();
if (firstMove == "Computer")
{
ComputerMove();
}
else
{
PlayerMove();
}
gameNumber += 1;
playAgain();
}
}
static void playAgain()
{
Console.Write("Do you wish to play again? (Y/N): ");
char playAgain = Convert.ToChar(Console.ReadLine());
Console.WriteLine();
if (playAgain == 'Y')
{
gameStatus = true;
}
else if (playAgain == 'N')
{
gameStatus = false;
}
}
In PlayingGame() you need to create a game loop.
static void PlayingGame()
{
int turn = // randomly choose 0 or 1 (whose turn is it)
bool finished = false;
while(!finished)
{
if(turn==0)
{
ComputerMove();
} else {
PlayerMove();
}
finished = CheckForEndOfGame();
turn = 1 - turn;
}
}
I do strongly suggest to create a Game class that handles the logic of the game and separate the UI (like messages etc) from the game mechanics. This is C# after all, and object oriented approaches are strongly encouraged.
You need to keep track of that game board and whose turn it is and what has been played in this Game class and display on the screen things based on the values (the state) of the game.

Getting an empty line being formed when day is Sunday (C#)

using System;
using System.Text.RegularExpressions;
namespace calendar
{
class Program
{
static void Main()
{
int year;
int day;
string[] month = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int[] days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Console.Write("Enter the year for which you wish to generate the calendar >> ");
int.TryParse(Console.ReadLine(), out year); // Validate //
Console.Write("Enter the day of the week that January first is on >> ");
int.TryParse(Console.ReadLine(), out day); // Validate //
while (day > 31 || day < 1) // Reprompt for value if date is out of range //
{
Console.WriteLine("Enter a valid date >> ");
Console.Write("Enter the day of the week that January first is on >> ");
int.TryParse(Console.ReadLine(), out day); // Validate //
}
switch (LeapYear(year)) // Switch statement checks if Leap Year is true //
{
case true:
days[1] += 1;
Console.WriteLine("Calendar for year - {0}", year);
for (int i = 0; i < month.Length; i++)
{
Console.WriteLine("\n" + month[i]);
day = DisplayCalender(days[i], day);
Console.Write("\n");
}
break;
}
}
public static int DisplayCalender(int days, int start) //Display Function//
{
int startDay = start;
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
for (int i = 0; i < start; i++)
Console.Write("\t");
for (int i = 1; i <= days; i++)
{
if (startDay > 6)
{
startDay = 0;
Console.WriteLine();
}
Console.Write(i + "\t");
startDay++;
}
return startDay;
}
public static Boolean LeapYear(int year)
{
if ((year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0))) // Checks each OR AND statements and return true or false //
{
return true;
}
else
return false;
}
}
}
I'm imagining the problem you're describing is having a month that starts on Sunday is making the calendar skip an entire line. Like the image:
That is happening because your method public static int DisplayCalender(int days, int start) is receiving the parameter int start with a value of 7.
That makes write tabs on the whole week on your first for and then skip the line on the second for.
To solve the issue you can simply reassign startDay with zero when it is 7 and check on you tab loop for startDay instead of start:
public static int DisplayCalender(int days, int start) //Display Function//
{
int startDay = start == 7 ? 0 : start;
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
for (int i = 0; i < startDay; i++)
Console.Write("\t");
for (int i = 1; i <= days; i++)
{
if (startDay > 6)
{
startDay = 0;
Console.WriteLine();
}
Console.Write(i + "\t");
startDay++;
}
return startDay;
}
This will give you the expected result:
What can you improve from here?
Making all that from scratch probably made you learn a lot about loop and flow. If you have some time check on DateTime.
There you have methods to find leap years, day of the week, and Month. That would help you simplify your code a lot.
Welcome to StackOverflow!
First I must say you are overcomplicating this very much, there is a perfect date library out of the box that you could have used.
For example:
DateTime now = DateTime.Now;
bool isLeapYear = DateTime.IsLeapYear(now.Year);
int daysInCurrentMonth = DateTime.DaysInMonth(now.Year, now.Month);
Instead of this:
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
for (int i = 0; i < start; i++)
Console.Write("\t");
Do this:
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
if (start < 7)
{
for (int i = 0; i < start; i++)
{
Console.Write("\t");
}
}
And instead of this:
if (startDay > 6)
{
startDay = 0;
Console.WriteLine();
}
do this:
if (startDay > 6)
{
startDay = 0;
if (i!=1)
{
Console.WriteLine();
}
}
So DisplayCalender method should look like this:
public static int DisplayCalender(int days, int start) //Display Function//
{
int startDay = start;
Console.WriteLine("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
if (start < 7)
{
for (int i = 0; i < start; i++)
{
Console.Write("\t");
}
}
for (int i = 1; i <= days; i++)
{
if (startDay > 6)
{
startDay = 0;
if (i != 1 )
{
Console.WriteLine();
}
}
Console.Write(i + "\t");
startDay++;
}
return startDay;
}

Array multiply pairs

I must create code in C# to:
Ask the user to enter an arbitrary set of numbers into an array and display all the entered numbers.
I done this part and the code is working properly
Then multiply pairs of numbers together and display the result. If you have an odd number of numbers then just display the last number.
E.G.
2 3 8 4 become 6 32
2 3 8 4 7 become 6 32 7
My problem is with the odd arrays. if the arrays has even number of element it's no problem, but if the arrays has odd number of elements there are errors, never print the last element.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayDisplay
{
class Array
{
static void Main(string[] args)
{
int[] array = new int[9];
int userInput = -1;
int itt = 0;
int count = 0;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
while (userInput != -1 && itt<array.Length)
{
array[itt] = userInput;
itt++;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
}
Console.WriteLine("The array contains: ");
for (int i = 0; i < array.Length; i++) {
Console.Write(" {0} , " ,array[i]);
}
Console.WriteLine("");
Console.WriteLine("count {0}",count-1);
if (count % 2 == 0)
{
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
else {
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1])
}
}
}
}
}
Because 7*0=0, try below:
if (count % 2 == 0)
{
for (int i = 0; i < array.Length - 1; i += 2)
{
Console.Write(" {0} , ", i == count - 2 ? array[i] : array[i] * array[i + 1]);
}
}
else
{
for (int i = 0; i < array.Length - 1; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
Try this:
int[] array = new int[9];
int userInput = -1;
int itt = 0;
int count = 0;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
while (userInput != -1 && itt < array.Length)
{
array[itt] = userInput;
itt++;
if (count == 9) break;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
}
Console.WriteLine("The array contains: ");
for (int i = 0; i < array.Length; i++)
{
Console.Write(" {0} , ", array[i]);
}
Console.WriteLine("");
Console.WriteLine("count {0}", count);
if (count % 2 == 0)
{
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
else
{
for (
int i = 0; i < array.Length; i += 1)
{
if (i == 7)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
break;
}
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
Console.ReadLine()
;

Stack overflow with a loop c#

I am currently having problems with this loop. It becomes an infinite loop and i get a stack overflow error. This is for a interest rate trade swap application. i is the length of the trade and l is the increasing index.
private void button1_Click(object sender, EventArgs e)
{
int outp = 0;
int i = int.Parse(tradeLength.Text);
string month = "January";
for (int l = 1; l <= i; l++)
{
Console.WriteLine("I iterated " + l + " Amount of times");
if (l == 1)
{
month = "January";
}
if (l == 2)
{
month = "February";
}
if (l == 3)
{
month = "March";
}
if (l == 4)
{
month = "Aprll";
}
if (l == 5)
{
month = "May";
}
if (l == 6)
{
month = "June";
}
if (l == 7)
{
month = "July";
}
if (l == 8)
{
month = "August";
}
if (l == 9)
{
month = "September";
}
if (l == 10)
{
month = "October";
}
if (l == 11)
{
month = "November";
}
if (l == 12)
{
month = "December";
}
else
{
month = "Null";
l = 1;
}
The cause is the final else:
if (l == 12) {
month = "December";
}
else { // <- if l != 12 (e.g. l == 1) restart the loop
month = "Null";
l = 1;
}
you want else if:
if (l == 1)
{
month = "January";
}
else if (l == 2)
{
...
}
...
else if (l == 12)
{
...
}
else {
month = "Null";
l = 1;
}
Edit: Another problem (see FKEinternet's comment) is a user input: if i is greater than 12 l never reaches it. You have to either validate the user input:
int i = int.Parse(tradeLength.Text);
if (i > 12)
i = 12; // or ask for other value
or use modular arithmetics:
for (int index = 1; index <= i; index++) {
int l = index % 12 + 1;
if (l == 1)
{
month = "January";
}
else if (l == 2)
...
else if (l == 12)
...
else
{
month = "Null";
l = 1;
}
}
It is not a very good idea to set the loop variable inside the loop. Like #stuartd pointed out, in your else line you set the loop variable to 1 and causing the loop to start all over again. Remove the l=1 line in your else block.
I presume you want to go to next year when i > 12. The way your code is made, when this happens, you get to loop forever, because "l" never reaches a number bigger than 12, it becomes 1 when it hits 13 and starts over.
To fix this, instead of
if (l == 1)
you want to use
if ((l % 12) == 1)
so your entire loop would be like this:
for (int l = 1; l <= i; l++)
{
Console.WriteLine("I iterated " + l + " Amount of times");
if ((l % 12) == 1)
{
month = "January";
}
if ((l % 12) == 2)
{
month = "February";
}
if ((l % 12) == 3)
{
month = "March";
}
if ((l % 12) == 4)
{
month = "Aprll";
}
if ((l % 12) == 5)
{
month = "May";
}
if ((l % 12) == 6)
{
month = "June";
}
if ((l % 12) == 7)
{
month = "July";
}
if ((l % 12) == 8)
{
month = "August";
}
if ((l % 12) == 9)
{
month = "September";
}
if ((l % 12) == 10)
{
month = "October";
}
if ((l % 12) == 11)
{
month = "November";
}
if ((l % 12) == 0)
{
month = "December";
}
{
PS = this is really not the right way to do this, I'm just using your own code and making the least amount of mods for it to work as intented. Good luck!

Need to convert Amount written in English language into Arabic language. Is there any possible way using C#?

I am trying to convert amount entered in digits into English language and it works fine. Now I want to convert it into Arabic language also. As user will type an amount in the text box it will convert it into English language or Arabic language on the basis of Culture code provided.
Below is my code to which runs on button click.
protected void btnConvert_Click(object sender, EventArgs e)
{
// CreateDatabase();
//txtBoxs.Text = sb.ToString();
string decimalPart = "";
string fractionPart = "";
string value = txtBoxs.Text;
string value1 = txtBoxs.Text;
string currency = "paisas";
string cultureCode = "ar-SA";
int index = 0;
if (value.Contains("."))
{
decimalPart = value.Substring(0, value.IndexOf("."));
fractionPart = value.Substring(value.IndexOf(".") + 1, value.Length - value.IndexOf(".") - 1);
if (cultureCode == "ar-SA")
{
//value = "";
//foreach (char c in decimalPart)
//{
// value += ToIndicDigits(c.ToString());
//}
//if (fractionPart != "")
//{
// value += ".";
// foreach (char c in fractionPart)
// {
// value += ToIndicDigits(c.ToString());
// }
//}
}
else
{
value = words(Convert.ToInt32(decimalPart));
if (fractionPart != "")
{
sb.Clear();
value += " and " + words(Convert.ToInt32(fractionPart)) + " " + currency;
}
}
}
else
{
if (cultureCode == "ar-SA")
{
//foreach (char c in value1)
//{
// value += ToIndicDigits(c.ToString());
//}
}
else
{
value = words(Convert.ToInt32(value));
index = value.ToString().LastIndexOf(" ");
int index2 = index > 0 ? value.LastIndexOf(" ", index - 1) : -1;
value = value.Insert(index2, " and ");
}
}
lblWords.InnerHtml = value;
}
Here is my method which i am using to convert amount into English language.
public string words(int number)
{
//int number = numbers;
if (number == 0)
return "Zero";
if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight";
int[] num = new int[4];
int first = 0;
int u, h, t;
if (number < 0)
{
sb.Append("Minus ");
number = -number;
}
string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ",
"Five " ,"Six ", "Seven ", "Eight ", "Nine "};
string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ",
"Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "};
string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ",
"Seventy ","Eighty ", "Ninety "};
string[] words3 = { "Thousand ", "Lakh ", "Crore " };
num[0] = number % 1000; // units
num[1] = number / 1000;
num[2] = number / 100000;
num[1] = num[1] - 100 * num[2]; // thousands
num[3] = number / 10000000; // crores
num[2] = num[2] - 100 * num[3]; // lakhs
for (int i = 3; i > 0; i--)
{
if (num[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (num[i] == 0)
continue;
u = num[i] % 10; // ones
t = num[i] / 10;
h = num[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
//if (h > 0 || i == 0)
// sb.Append("and ");
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
}

Categories