Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using System; namespace BigFactorial { class Program { static void Main ( string [ ] args ) { Console.WriteLine ( Enter a number to

using System;
namespace BigFactorial
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number to compute its factorial:");
int n = int.Parse(Console.ReadLine());
int[] factorial = new int[5001];
factorial[0]=1;
int length =1;
while (n >=0)
{
length = ComputeFactorial(n, factorial);
Console.WriteLine("The factorial of "+ n +" is:");
Console.WriteLine("Number of digits: "+ length);
DisplayFactorial(factorial, length);
Console.WriteLine("
Enter a number to compute its factorial (negative to exit):");
n = int.Parse(Console.ReadLine());
}
}
static int ComputeFactorial(int n, int[] factorial)
{
int carry =0;
for (int i =0; i < factorial.Length; i++)
{
factorial[i]= factorial[i]* n + carry;
carry = factorial[i]/1000000;
factorial[i]%=1000000;
if (factorial[i]>0 && carry ==0) break;
}
int length = factorial.Length;
while (length >1 && factorial[length -1]==0)
length--;
return length;
}
static void DisplayFactorial(int[] factorial, int length)
{
Console.Write(factorial[length -1]);
for (int i = length -2; i >=0; i--)
{
Console.Write(String.Format("{0:000000}", factorial[i]));
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions