Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C# program that asks a user for a whole number and prints out the factor

ID: 3801514 • Letter: W

Question

Write a C# program that asks a user for a whole number and prints out the factorial of that number. The program should use three threads (or tasks) to complete the computation. One thread computes the factorial first third, the second thread should compute the factorial of the middle third, and the third thread computes factorial of the last third. A final thread multiplies all three results to get the final answer. For example, to compute 251: 25 divided into three approximately equal parts = 8 + 8 + 9 Thread 1 (or Task 1): Result 1 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 Thread 2 (or Task 2): Result 2 = 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 Thread 3 (or Task 3): Result 3 = 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 * 25 Final result (Thread/Task 4) = Result 1 * Result 3 Print Final result. Compare the result of computing the factorial by using a sequential loop and printing the answer. Which method is faster? How did you determine which method is faster?

Explanation / Answer

Hi,

Please find below both the versions. I have created a program that takes a user input in variable 'num' and then splits the number into 3 sets and calculate the factorial for individual set. Finally, the individual calculated numbers are multiplied to get the complete factorial.

Required factorial Program with 3 threads-

using System.IO;
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the number");
int num = int.Parse(Console.ReadLine());
int st1=1;
int st2=(num/3)+1;
int st3=((num/3)*2)+1;
int maxi1=num/3;
int maxi2=((num/3)*2);
int maxi3=num;
Console.WriteLine(st1);
Console.WriteLine(st2);
Console.WriteLine(st3);
Console.WriteLine(maxi1);
Console.WriteLine(maxi2);
Console.WriteLine(maxi3);
int f1=thread1(st1,maxi1);
Console.WriteLine(f1);
int f2=thread2(st2,maxi2);
Console.WriteLine(f2);
int f3=thread3(st3,maxi3);
Console.WriteLine(f3);
int factorial=(f1*f2*f3);
Console.WriteLine(factorial);
}
static int thread1(int st1,int maxi1)
{
int fact1 = 1;
for (int i = st1; i <= maxi1; i++)
{
fact1 *= i;
}
return fact1;
}
static int thread2(int st2,int maxi2)
{
int fact1 = 1;
for (int i = st2; i <= maxi2; i++)
{
fact1 *= i;
}
return fact1;
}
static int thread3(int st3,int maxi3)
{
int fact1 = 1;
for (int i = st3; i <= maxi3; i++)
{
fact1 *= i;
}
return fact1;
}
}

Basic Factorial Program using loop-

using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter the number");
int p = int.Parse(Console.ReadLine());
int fact = 1;
for (int i = 1; i <= p; i++)
{
fact *= i;
}

Console.WriteLine(fact);
}
}

Method 1 which splits the number and then calculate the factorial is faster.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote