# find factorial of a number

Page 1 of 1

## 4 Replies - 27084 Views - Last Post: 09 July 2010 - 08:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=180858&amp;s=fc4063804c4b080f1e0a890d61c3d1ba&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aarti ayyar

Reputation: -6
• Posts: 11
• Joined: 04-July 10

# find factorial of a number

Posted 09 July 2010 - 06:03 AM

hey guys help me with finding factorial of a program...i have made a program but it is showing a wrong output....

my program is as follows

```int a,b;
{
if(a==1)
{
Console.WriteLine(1);
}
else
{
b=(a-1)*a;
Console.WriteLine( b );
}
}
```

This post has been edited by JackOfAllTrades: 09 July 2010 - 06:19 AM
Reason for edit:: Added code tags for the umpteenth time.

Is This A Good Question/Topic? -1

## Replies To: find factorial of a number

### #2 Christos75

Reputation: -4
• Posts: 55
• Joined: 06-October 09

## Re: find factorial of a number

Posted 09 July 2010 - 06:22 AM

You need something like that:

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

```

### #3 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4646
• Posts: 8,203
• Joined: 08-June 10

## Re: find factorial of a number

Posted 09 July 2010 - 07:11 AM

Since this is so obviously a school assignment, I wouldn't just post code for you to copy and paste.

I'd suggest that you think of the logic of factorials. Say you have number X:
Is X > 1?
--Yes
----Then Factorial = X * !(X - 1)
--No
----Then Factorial = 1

How do you achieve something like this? Has your class covered recursion yet? If no, it is also achievable using a loop.

Your code only does the first step. All you're doing is X * (X-1). You need to get the factorial of (X - 1).

The following examples are obviously pseudocode.

So if you have learned recursion, you could use a function:
```f(x) = { x > 1 | x * f(x-1)
x = 1 | 1
```

If you havent, you could use a loop that counts:
```fact = 1
iterator = 1
loop from iterator to x
fact = fact * iterator
```

So without giving you code, you could have solved this one yourself.

This post has been edited by insertAlias: 09 July 2010 - 07:12 AM

### #4 tlhIn`toq

Reputation: 5871
• Posts: 12,786
• Joined: 02-June 10

## Re: find factorial of a number

Posted 09 July 2010 - 07:56 AM

This post is worse than your others of nearly the exact same issue...
No description of the actual problem just "wrong output". Guess what: The output isn't wrong. It is exactly what you tell the computer to give you. you just aren't making much of an effort. You are making a habit out of writing 10 lines of code to meet the site requirement, then posting it here so someone will do your schoolwork for you.

If you don't want to be in computer programming class then drop the class.
If you do have an actual interest in this sort of then, then make an effort.

### #5 Imdsm

• D.I.C Regular

Reputation: 103
• Posts: 362
• Joined: 21-March 09

## Re: find factorial of a number

Posted 09 July 2010 - 08:27 AM