Visual Studio Code C# Well Document everyline saying what the code does. Include
ID: 3752065 • Letter: V
Question
Visual Studio Code C#
Well Document everyline saying what the code does.
Include designer code and code!
Extra 5-1 Calculate the factorial of a number In this exercise, you'l1 create a form that accepts an integer from the user and then calculates the factorial of that integer Factorial Calculator Number Factorial: 2432.902.008,176.640,000 Calculate Ext The factorial of an integer is that integer multiplied by every positive integer less than itself. A factorial number is identified by an exclamation point following the number. Here's how you calculate the factorial of the numbers 1 through 5 2!-21 3! 3*2 * 1 4!4 3* 2*1 5!-5 * 4*3 *2 * 1 which equals 1 which equals 2 which equals 6 which equals 24 which equals 120 To be able to store the large integer values for the factorial, this application can't use the Int32 data type 1. Start a new project named Factorial in the Extra Exercises Chapter 05 Factorial 2. Add labels, text boxes, and buttons to the default form and set the properties of directory the form and its controls so they appear as shown above. When the user presses the Enter key, the Click event of the Calculate button should fire. When the user presses the Esc key, the Click event of the Exit button should fire Create an event handler for the Click event of the Calculate button. This event handler should get the number the user enters, calculate the factorial of that number, display the factorial with commas but no decimal places, and move the focus to the Number text box. It should return an accurate value for integers fronm 1 to 20. (The factorial of the number 20 is shown in the form above.) Create an event handler for the Click event of the Exit button that closes the form. 3. 4. 5. Test the application to be sure it works correctlyExplanation / Answer
Program.cs
using FactorialCalculator.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
namespace FactorialCalculator
{
public class Program
{
static void Main(string[] args)
{
// -- Check if any params are passed
if (args.Count() > 0)
{
Console.WriteLine("Factorial Calculator");
Console.WriteLine();
for (int i = 0; i < args.Count(); i++)
{
try
{
// -- Validate
BigInteger n = Factorial.ValidateInput(args[i]);
// -- Calculate
n = Factorial.Calculate(n);
Console.WriteLine("{0}! = {1}", args[i], n);
}
catch (InvalidInputException ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
catch (FactorialException fex)
{
Console.WriteLine("Error: {0}", fex.Message);
}
}
Console.WriteLine("Execution completed.");
Console.WriteLine();
Console.ReadLine();
}
else
{
Console.WriteLine("Factorial Calculator (n!)");
Console.WriteLine("Please enter a number:");
Console.WriteLine();
// -- This while loop allows the user to enter as many numbers to calculate the factorial of the given number
// -- The 'exit' command can be used to exit the process
while(true)
{
// -- Read input
var input = Console.ReadLine();
// -- Exit if the input == 'exit'
if (input == "exit") break;
try
{
// -- Validate
BigInteger n = Factorial.ValidateInput(input);
// -- Calculate
n = Factorial.Calculate(n);
Console.WriteLine("{0}! = {1}", input, n);
Console.WriteLine();
}
catch (InvalidInputException ex)
{
Console.WriteLine("Error: {0}", ex.Message);
Console.WriteLine();
}
catch (FactorialException fex)
{
Console.WriteLine("Error: {0}", fex.Message);
Console.WriteLine();
}
}
}
}
}
}
Factorial.cs
using FactorialCalculator.Exceptions;
using System.Numerics;
namespace FactorialCalculator
{
/// <summary>
/// Handles all things related to Factorial Calculation and Validation
/// </summary>
public static class Factorial
{
/// <summary>
/// Calculate n! recursively
/// </summary>
/// <param name="n">Value to calculate</param>
/// <returns>Factorial of n</returns>
/// <exception cref="FactorialException">Thrown when the value of n is not valid to calculate factorial.</exception>
public static BigInteger Calculate(BigInteger n)
{
// -- Based on rules of Factorial calculation, if n = 0 then 0! = 1
if (n == 0) return 1;
// -- Cannot calculate negative value
if (n < 0) throw new FactorialException(string.Format("{0} is undefined", n));
return n * Calculate(n - 1);
}
/// <summary>
/// Validate input of the console application.
/// </summary>
/// <param name="input">Standard input</param>
/// <returns></returns>
/// <exception cref="InvalidInputException"></exception>
public static BigInteger ValidateInput(string input)
{
// -- Assign 'out' value
var n = new BigInteger(0);
// -- Check if the value is actually a bigint
var valid = BigInteger.TryParse(input, out n);
// -- There could be a better solution for this, but check the length of the input otherwise a stack overflow exception will occur
if(input.Length >= 4)
{
throw new InvalidInputException("I do not have enough memory to calculate this.");
}
// if the input is invalid, throw exception to calling method.
if (!valid)
{
throw new InvalidInputException(string.Format("Invalid input '{0}'", input));
}
return n;
}
}
}
FactorialCalculator.csproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E4A1BBE1-0725-48C3-9312-E86DDD0462EB}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FactorialCalculator</RootNamespace>
<AssemblyName>FactorialCalculator</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<PublishUrl>publish</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>binDebug</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>binRelease</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ExceptionsFactorialException.cs" />
<Compile Include="ExceptionsInvalidInputException.cs" />
<Compile Include="Factorial.cs" />
<Compile Include="Program.cs" />
<Compile Include="PropertiesAssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.