Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to print following factorial series in C#...
1!+2!+3!+4!.....
and
1/1!+1/2!+1/3!+1/4!....

Plz help out..
Posted
Updated 23-Jul-20 3:59am
Comments
/\jmot 9-Dec-14 1:03am    
Not clear?
be more specific..
Member 11242836 9-Dec-14 1:19am    
I wanted to get sum of those factorials.
ex. 1!+2! = 3.
so till the value given by user..I want the sum till that..
Shanish K 9-Dec-14 1:04am    
Do you have to print just the series, or the result as well? Can you please elaborate how the result should be?
Member 11242836 9-Dec-14 1:17am    
I wanted the result as well...the result should br the sum of those factorials
Thnk u
_Maxxx_ 9-Dec-14 1:08am    
It is expected that you make some attempt before asking people to help you do your homework

Write a method that calculates factorial of a given number. That's the most important thing. Next step is to find the value of a given expression. If it's just calculating the factorial 4 times, call the method 4 times. If it's some kind of series, write another method where you will pass 'n' where n is the number till which the factorial will be calculated. In this method you will call the first method you wrote n times using a loop (while / for).
You gotta BEGIN first.
 
Share this answer
 
Comments
Raul Iloc 9-Dec-14 1:36am    
I do not agree with you, that you should invoke the factorial method "n" times for a series is an optimal solution. Look on my solution above and you could see the optimal solution that is doing the computation of all series in one loop.
Ankur\m/ 9-Dec-14 1:54am    
Your solution isn't "Reusable". Reusability principle says that you should create methods for reusable codes.
Code to find a Factorial will be same. So you can create a method for it and you don't need to write it every time a new type of expression is given.
Raul Iloc 9-Dec-14 2:15am    
Is not like this! My entire code, for each series, could be put into a single method that should have one param "n".
Ankur\m/ 9-Dec-14 2:22am    
And that is what I suggested. First thing I said was to create a method to calculate factorial of a number. And then depending on the expression, next approach should be decided.
Raul Iloc 9-Dec-14 2:42am    
No, my suggestion is to use only one method for computing the entire series and not to use "n" time a method for each factorial!
1.For first expression you should do like in the next example:
C#
int n = 10;
            StringBuilder builder = new StringBuilder();
            double resultValue = 0;
            double temp = 1;
            for (int i = 0; i < n; i++)
            {
                if (i > 0)
                    builder.Append(" + ");
                builder.Append(string.Format("{0}!", i + 1));
                temp *= (i + 1);
                resultValue += temp;
            }
            //
            builder.Append(string.Format(" = {0}", resultValue));
            _textBox.Text = builder.ToString();


2.For 2nd expression a similar code like below:
C#
int n = 10;
            StringBuilder builder = new StringBuilder();
            double resultValue = 0;
            double temp = 1;
            for (int i = 0; i < n; i++)
            {
                if (i > 0)
                    builder.Append(" + ");
                builder.Append(string.Format("1/{0}!", i + 1));
                temp *= (i + 1);
                resultValue += 1/temp;
            }
            //
            builder.Append(string.Format(" = {0}", Math.Round(resultValue, 2)));
            _textBox.Text = builder.ToString();
 
Share this answer
 
v2
Comments
Ankur\m/ 9-Dec-14 1:28am    
He needs to calculate factorial for a given series. He doesn't have to create a "string". I can be sure of this because he replied to my comment where I suggested him on how to go about it.
Raul Iloc 9-Dec-14 1:32am    
From it original question he ask about printing this factorial, anyway I updated my solution to also compute both series.
Member 11242836 9-Dec-14 1:43am    
Thnk u...
Raul Iloc 9-Dec-14 1:44am    
Welcome!
Check this code hope this will help you.

C#
//The fucntion can be called in page load or in button click or as you like.
 protected void Page_Load(object sender, EventArgs e)
    {
       // Call your fucntion here and pass your input to this method
        String Factorials = YourFactorial(3);
    }

 public String YourFactorial(int n)
    {

        if (n <= 1)
        {
            return " 1! :" + " The result is : 1" ;
        }

        int result = 1;

        String results = "";
        results = "1! + ";
        for (int i = 2; i <= n; i++)
        {
            results =results + i.ToString()+" ! + ";
            result = result * i;

        }

        return results + " The result is :" + result;

    }
 
Share this answer
 
Comments
Member 11242836 9-Dec-14 1:43am    
Thnk u...
Ankur\m/ 9-Dec-14 1:56am    
I already described this technique in my answer. Let him try. You writing code for him isn't going to help him.
syed shanu 9-Dec-14 1:59am    
Hi,
Well you can see my comment for him.as he again requested for the solution i did for him this is very simple solution for us but for starter it seems hard
Ankur\m/ 9-Dec-14 2:01am    
And you writing code for him isn't going to make him learn. Let him try and show what he did and where he got stuck. Then you could have helped him with that.
syed shanu 9-Dec-14 2:02am    
Cool

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900