find factorial of a number

find factorial of a number using if else

Page 1 of 1

4 Replies - 26064 Views - Last Post: 09 July 2010 - 08:27 AM Rate Topic: -----

#1 aarti ayyar  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • 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;
{
console.WriteLine("please enter a number");
a=Convert.ToInt 32(console.ReadLine());
if(a==1)
{
Console.WriteLine(1);
}
else
{
b=(a-1)*a;
Console.WriteLine( b );
}
Console.ReadLine();
}

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  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • 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 ");
            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);
        }
    }
}


Was This Post Helpful? -1
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5632
  • View blog
  • Posts: 12,080
  • 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...
Not so much as a "please"... just an order to help you with it.
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.
Was This Post Helpful? 0
  • +
  • -

#5 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: find factorial of a number

Posted 09 July 2010 - 08:27 AM

Here's another link to help you brush up on your maths:

http://en.wikipedia.org/wiki/Factorial
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1