Sie sind auf Seite 1von 7

Factorial of a number | The ASP.NET Forums https://forums.asp.net/t/1801249.aspx?

Factorial+of+a+number

ASP.NET Forums / Community / Free For All / Factorial of a number

Factorial of a number [Answered]
13 replies
Last post May 08, 2012 01:03 PM by AccuWebHosting.Com

Factorial of a number
May 08, 2012 02:03 AM | .net_junkie

This is my code to find a factorial

int Number=1;
Number=Convert.ToInt32(TextBox1.Text);
if(Number==0)
Label1.Text="enter a number greater than 0";
else

//int functionReturnValue = 0;
//functionReturnValue = 1;
for (int i = 0; i >Number-1; i--)
{
Number=Number*i;

}
Label1.Text=Convert.ToString(Number);
}
}

But i am unable to get the result help me with this??

Re: Factorial of a number


May 08, 2012 02:11 AM | karthicks

hi, refer below code

int Number=1; 
                    Number=Convert.ToInt32(TextBox1.Text); 
                  if(Number==0) 
                      Label1.Text="enter a number greater than 0"; 
                  else 
 

1 of 7 20-03-2019, 23:14
Factorial of a number | The ASP.NET Forums https://forums.asp.net/t/1801249.aspx?Factorial+of+a+number

                    //int functionReturnValue = 0; 


                    //functionReturnValue = 1; 
                    for (int i = Number; i >0; i--) 
                    { 
                       Number=Number*i; 
            
                    } 
                   Label1.Text=Convert.ToString(Number); 
                } 
  }

Re: Factorial of a number


May 08, 2012 02:13 AM | kirupa.v

Hi,

Try the following code

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

namespace Factorial
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Give me the number you want the factorial: \n ");
long a = Convert.ToInt64(Console.ReadLine());
Console.WriteLine(" The factorial of given number is: " + Factorial(a));
Console.ReadKey();
}

public static long Factorial(long number)


{
if (number <= 1)
return 1;
else
return number * Factorial(number - 1);
}
}
}

Reply me for any issues..

2 of 7 20-03-2019, 23:14
Factorial of a number | The ASP.NET Forums https://forums.asp.net/t/1801249.aspx?Factorial+of+a+number

Re: Factorial of a number


May 08, 2012 02:23 AM | tusharrs

protected void Page_Load(object sender, EventArgs e)


{

long Number = 1;
Number = Convert.ToInt64(TextBox1.Text);
if (Number == 0)
Label1.Text = "enter a number greater than 0";
else
{
Number = Factorial(Number);
}
Label1.Text = Convert.ToString(Number);

static long Factorial(long number)


{
if (number <= 1)
return 1;
else
return number * Factorial(number - 1);
}

Re: Factorial of a number


May 08, 2012 02:29 AM | .net_junkie

with your code i am getting the result as 0

and i want the code with recursion also can anyone provide me the code??

Re: Factorial of a number


May 08, 2012 02:30 AM | kirupa.v

Hi,

Mine will give u the required result..

You can try with that ..

3 of 7 20-03-2019, 23:14
Factorial of a number | The ASP.NET Forums https://forums.asp.net/t/1801249.aspx?Factorial+of+a+number

Re: Factorial of a number


May 08, 2012 02:57 AM | .net_junkie

tusharrs

static long Factorial(long number)


{
if (number <= 1)
return 1;
else
return number * Factorial(number - 1);
}

hi can you explain me this logic??what really Factorial doing?

Re: Factorial of a number


May 08, 2012 03:13 AM | kirupa.v

Hi,

Actually recurssively the process occurs..

For eg::

the input no : is 3

Initially it enters inside the else part

the return part will be 3*Factorial (3-1)     (i.e..) it ll be 3*Factorial(2)

and now for Factorial(2) the same process will occur until ot enters the if block the same recursive process will
continue and u ll get the required output.

Re: Factorial of a number


May 08, 2012 03:28 AM | tusharrs

if you pass 6 to the function

then it checks whehter it is <=1 then its no

4 of 7 20-03-2019, 23:14
Factorial of a number | The ASP.NET Forums https://forums.asp.net/t/1801249.aspx?Factorial+of+a+number

so it goes in else condition and in return statement its

number * factorial(number-1)

means number multiplied by the result of factorial(6-1) i.e factorial (5)

so it again calls factorial function with parameter 5

this way it calls the function till it goes to factorial(0)

and in factorial(0) it returns 1

and now it executes only the return statements so

finally 6 * factorial(5) i.e. 120 * factorial(4) i.e. 24 * factorial(3) i.e. 6 * factorial(2) i.e. 2 * factorail(1) i.e. 1

the result is 720

Re: Factorial of a number


May 08, 2012 05:34 AM | .net_junkie

So factorial is a method and we are using this code  in other part and we are passing aprameters isnt it?

Re: Factorial of a number


May 08, 2012 05:37 AM | tusharrs

factorial is a function which takes a parameter of datatype long and returns the factorial of datatype long

Re: Factorial of a number


May 08, 2012 05:44 AM | kirupa.v

Hi,

Factorial is a function and u need to pass number as paramater to it..

5 of 7 20-03-2019, 23:14
Factorial of a number | The ASP.NET Forums https://forums.asp.net/t/1801249.aspx?Factorial+of+a+number

Re: Factorial of a number


May 08, 2012 12:06 PM | cYpH3r x3r0

your loop condition is wrong it should be i<number, go on and store result in some third variable

Re: Factorial of a number


May 08, 2012 01:03 PM | AccuWebHosting.Com

Hi!

This is code to find Factorial of given number using Loop:

static int Factorial(int n)

if (n <= 1)

return 1;

int result = 1;

for (int i = 2; i <= n; i++)

result = result * i;

return result;

To find the factorial of given number you can use Recursive Function also :

static int Fact(int n)

if (n <= 1)

return 1;

return n * Fact(n - 1);

6 of 7 20-03-2019, 23:14
Factorial of a number | The ASP.NET Forums https://forums.asp.net/t/1801249.aspx?Factorial+of+a+number

Regards.

This site is managed for Microsoft by Neudesic, LLC. | © 2019 Microsoft. All rights reserved.

7 of 7 20-03-2019, 23:14

Das könnte Ihnen auch gefallen