C#:
using static System.Console;
namespace WritingFunctions
{
class Program
{
static double Factorial(double number)
{
if (number < 1)
{
return 0;
}else if (number == 1)
{
return 1;
}
else
{
return number * Factorial(number - 1);
}
}
static void RunFactorial()
{
Write("Enter a number (Between 0 and 170): ");
if(double.TryParse(ReadLine(), out double number))
{
WriteLine($"{number:N0}! = {Factorial(number):N0}");
}
else
{
WriteLine("You did not enter a valid number.");
}
}
static string CardinalToOrdinal(int number)
{
switch (number)
{
case 11:
case 12:
case 13:
return $"{number}th";
default:
string numberAsText = number.ToString();
char lastDigit = numberAsText[numberAsText.Length - 1];
string suffix = string.Empty;
switch (lastDigit)
{
case '1':
suffix = "st";
break;
case '2':
suffix = "nd";
break;
case '3':
suffix = "rd";
break;
default:
suffix = "th";
break;
}
return $"{number}{suffix}";
}
}
static void RunCardinalToOrdinal()
{
for (int number = 1; number <= 50; number++)
{
Write($"{CardinalToOrdinal(number)} ");
}
}
static decimal SalesTax (decimal amount, string twoLetterRegionCode)
{
decimal rate = 0.0M;
switch (twoLetterRegionCode)
{
case "CH": // Switzerland
rate = 0.08M;
break;
case "DK": // Denmark
case "NO": // Norway
rate = 0.25M;
break;
case "GB": // (Now Dead) Great Britain
case "FR": // France
rate = 0.2M;
break;
case "HU": // Hungary
rate = 0.27M;
break;
case "OR": // Oregon
case "AK": // Alaska
case "MT": //Monatana
rate = 0.0M;
break;
case "ND": //North Dakota
case "WI": // Wisconsin
case "ME": // Maryland
case "VA": //Virginia
rate = 0.05M;
break;
case "CA": // California
rate = 0.0825M;
break;
default: // Most US States
rate = 0.06M;
break;
}
return amount * rate;
}
static void RunSalesTax()
{
Write("Enter an amount: ");
string amountintext = ReadLine();
Write("Enter a two letter region code: ");
string region = ReadLine();
if (decimal.TryParse(amountintext, out decimal amount))
{
decimal taxToPay = SalesTax(amount, region);
WriteLine($"You must pay {taxToPay} in sales tax.");
}
else
{
WriteLine("You did not a enter a valid amount.");
}
}
static void TimesTable (byte number)
{
WriteLine($"This is the {number} times table");
for (int row = 1; row <= 12; row++)
{
WriteLine($"{row} X {number} = {row * number}");
}
}
static void RunTimesTable()
{
Write("Enter a number between 0 and 255: ");
if(byte.TryParse(ReadLine(), out byte number))
{
TimesTable(number);
}
else
{
WriteLine("You did not enter a valid number.");
}
}
static void Main(string[] args)
{
//RunTimesTable();
//RunSalesTax();
//RunCardinalToOrdinal();
RunFactorial();
}
}
}
In the above code taken from a book i am currently reading, the output is supposed to be some file named log.txt to be formed, but it is ot happening with me.
please help
OS: Windows 10
Book: c# 7.1 and dotnet core 2.0 modern cross platform development by Mark J. Price
Last edited by a moderator: