Find a sum of series 1by1Fact plus 1by3Fact

Find a sum of series 1by1Fact plus 1by3Fact in C#





public void CalculateSumOfFractionfactorial() {
// 1/1! + 1/3! + 1/5!
double sum = 0;double fact = 1;int n = 3,k=1;
for (int i = 1; i <= n; i++)
{
fact = 1;
//Factorial of number
for (int j = 1; j <= k; j++)
{
fact = fact * j;
}
sum = sum + (1 / fact);
k = k + 2;
}

}


Dry run


1.   public void CalculateSumOfFractionfactorial() {
2.   // 1/1! + 1/3! + 1/5!
3.   double sum = 0;double fact = 1;int n = 3,k=1;
4.   for (int i = 1; i <= n; i++)
5.   {
6.   fact = 1;
7.   //Factorial of number
8.   for (int j = 1; j <= k; j++)
9.   {
10.  fact = fact * j;
11.  }
12.  sum = sum + (1 / fact);
13.  k = k + 2;
14.  }
15.   
16.  }
17.   

Output
1.175


Dry run in excel - Trace table



Share on Google Plus

About myzingonline

Myzingonline is all about zing to share experience, knowledge, likes, dislikes and ideas of a person to the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment