Write a C# program that asks a user for a whole number and prints out the factor
ID: 3864518 • 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 up to 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 25!:
25 divided into three approximately equal parts = 8+8+9
Thread1 (or Task1): Result1 = 1*2*3*4*5*6*7*8
Thread2 (or Task2): Result2 = 9*10*11*12*13*14*15*16
Thread3 (or Task3): Result3 = 17*18*19*20*21*22*23*24*25
Final result (Thread/Task4) = Result1*Result2*Result3
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?
Please help me fix my code? Is there something wrong with it?
Program.cs" x MidtermExam factorial Program 1 Eusing System 2 using System Collections .Generic; 3 using System. Linq 4 using System.Text; 5 using System.Threading 8 Enamespace factorial references class Program 11 static void Main string[] args) int i j, k, number, fact1, fact2, fact3. int divideNum; int result 15 Console.WriteLine ("Enter the Number"); 17 number int.Parse(Console.ReadLine()); //divide the user input by 3. divideNum number 3; 21 Thread thread1 new Thread (new ThreadStart (A)); Thread thread2 new Thread (new ThreadStart(B); thread1. Start thread2 start(); thread 3. start(); 27 thread 1. Join(); thread2. Join thread3. Join 31 32 //to get the second half we need to increment one number to first part 33 divideNum divideNum 1; 35 37 //this A funtion is used to compute factorial or first part 39 static void A() 40 E Sleep (50) Thread 90 Main (string[] args)Explanation / Answer
#include<stdio.h>
int n1,n2,n3;int factorial_1(int x)
{
int i;
long int result = 1;
for(i = x ; i>0 ; i--)
{
result = result * i;
}
return result;
}
int factorial_2(int x)
{
int i;
long int result = 1;
for(i = n1+1 ; i <= x + n1 ; i++)
{
result = result * i;
}
return result;
}
int factorial_3(int x)
{
int i;
long int result = 1;
for(i = n1+n2+1 ; i <= x+n2+n1 ; i++)
{
result = result * i;
}
return result;
}
int main()
{
int num,rem;
printf("enter a value ");
scanf("%d",&num);
n1 = num/3;
num = num - n1;
n2 = num/2;
n3 = num - n2;
printf("the result is : %ld ",factorial_1(n1) * factorial_2(n2) *factorial_3(n3));
return 0;
}
===============> OUTPUT <===================
enter a value
9
the result is : 362880
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.