tags:

views:

136

answers:

4

I'm having a logic problem here.. i want to add the result of the factorial values.. how do i add them.. here's my code..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task_8_Set_III
{
    class Program                       
     {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 7; i++)
            {
                double c = i / fact(i);

                Console.WriteLine("Factorial is : " + c);
                Console.ReadLine();
                Console.WriteLine("By Adding.. will give " +);

            }
        }
        static double fact(double value)
        {
            if (value ==1)
            {
                return 1;
            }
            else
            {
                return (value * (fact(value - 1)));
            }
        }
    }
}
+1  A: 

You need to add a total variable to keep track of the sum.

double total = 0; //the total

for (int i = 1; i <= 7; i++)
{
    double c = i / fact(i);
    total += c; // build up the value each time
    Console.WriteLine("Factorial is : " + c);
    Console.ReadLine();
    Console.WriteLine("By Adding.. will give " + total);

}
jjnguy
I don't quite understand "c = i / fact(i)"....if c is the factorial calculation, should it not just be "c = fact(i)"?
Aaron
that is what the OP had...I was just gonna leave it.
jjnguy
the question was that.. i wanted to sum (1/1!)+(2/2!)+... (7/7!)..
Abid
c is in the loop..c= i/fact(i) will iterate..
Abid
Then I'd be a little more careful in how you word your Console.WriteLine statements...c is clearly not i! but is rather (i/i!).
Aaron
+1  A: 

Not sure if this is what you meant but if for factorial of N you want to have the sum of all factorials up to that value this is how you do it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task_8_Set_III
{
    class Program                       
     {
        static void Main(string[] args)
        {
            double sum = 0;
            for (int i = 1; i <= 7; i++)
            {
                double c = i / fact(i);
                sum += c;
                Console.WriteLine("Factorial is : " + c);
                Console.ReadLine();
                Console.WriteLine("By Adding.. will give " + sum);

            }
        }
        static double fact(double value)
        {
            if (value ==1)
            {
                return 1;
            }
            else
            {
                return (value * (fact(value - 1)));
            }
        }
    }
}
ruibm
This will not return the sum of the factorials...this is returning the sum of (i/i!)...unless that's what OP originally intended...
Aaron
True lol, but this is prob what they need otherwise the line `Console.WriteLine("Factorial is : " + c);` is also wrong.
ruibm
A: 

short of totally understanding what you want to do exactly, here's two things...

  • in programming, the following expression is perfectly valid: i = i + 1 Saying "the new value of i is the old value of i plus one"
  • variables live within scopes, their boundaries usually being braces { }, that is you will need a variable that is outside the braces of the foreach in order to "remember" stuff of the previous iteration.
flq
More specifically, the first point you mention is valid for *most* programming languages. However, `i = i + 1` might be not so permissible in functional programming.
stakx
A: 
   static void Main(string[] args)
            {
                int sum = 0;
                for (int i = 1; i <= 7; i++)
                {
                    int c = fact(i);
                    sum += c;
                    Console.WriteLine("Factorial is : " + c);
                    Console.ReadLine();
                    Console.WriteLine("By Adding.. will give " + sum);

                }
            }
            static int fact(int value)
            {
                if (value ==1)
                {
                    return 1;
                }
                else
                {
                    return (value * (fact(value - 1)));
                }
            }
Aaron