# Factorial of a number RSS

## 13 replies

Last post May 08, 2012 05:03 PM by AccuWebHosting.Com

Member

297 Points

825 Posts

### Factorial of a number

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??

It is not about what caused you to fall flat on your back that defines you; it is what you do to get back up on your feet.

All-Star

31334 Points

5414 Posts

### Re: Factorial of a number

hi, refer below code

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 = Number; i >0; i--)

{

Number=Number*i;

}

Label1.Text=Convert.ToString(Number);

}

}

Thanks,
Karthick S

Contributor

2070 Points

531 Posts

### Re: Factorial of a number

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 ");
Console.WriteLine(" The factorial of given number is: " + Factorial(a));
}

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

Contributor

3230 Points

668 Posts

### Re: Factorial of a number

```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);
}```

Regards

( Mark as Answer if it helps you out )

View my Blog
• Edited by tusharrs on May 08, 2012 06:28 AM
• Marked as answer by .net_junkie on May 09, 2012 07:51 AM

Member

297 Points

825 Posts

### Re: Factorial of a number

with your code i am getting the result as 0

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

It is not about what caused you to fall flat on your back that defines you; it is what you do to get back up on your feet.

Contributor

2070 Points

531 Posts

### Re: Factorial of a number

Hi,

Mine will give u the required result..

You can try with that ..

Member

297 Points

825 Posts

### Re: Factorial of a number

#### 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?

It is not about what caused you to fall flat on your back that defines you; it is what you do to get back up on your feet.

Contributor

2070 Points

531 Posts

### Re: Factorial of a number

Hi,

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.

Contributor

3230 Points

668 Posts

### Re: Factorial of a number

if you pass 6 to the function

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

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

Regards

( Mark as Answer if it helps you out )

View my Blog

Member

297 Points

825 Posts