S.O.S. Mathematics CyberBoard

Your Resource for mathematics help on the web!
It is currently Wed, 22 May 2013 03:48:28 UTC

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Factorial Function
PostPosted: Tue, 1 Dec 2009 10:27:34 UTC 
Offline
S.O.S. Newbie

Joined: Tue, 1 Dec 2009 10:13:56 UTC
Posts: 2
Hi, need help with the following. Can someone please help me. If possible also explanation to the answer:

Q. Write a function ‘factorial’ that takes in one input:

int num

and returns the factorial of the given number.

If the function is passed 0 or a negative number then your
function should return 0.

Thanks and Regards
Nirmala


Last edited by nirmala on Mon, 10 May 2010 16:38:03 UTC, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue, 1 Dec 2009 11:17:11 UTC 
Offline
Site Admin
User avatar

Joined: Thu, 15 Feb 2007 06:35:15 UTC
Posts: 755
seems like you don't really understand what you are doing since this is a relatively trivial problem. Also it is not "other topics in advanced mathematics" so I moved it to Computer science...

one example might be

Code:
factorial(n)
     if n<1
       return 0
     else
       fact := 1
       for i := 1-->n
          fact = fact*i
     return fact

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue, 1 Dec 2009 19:55:07 UTC 
Offline
Moderator
User avatar

Joined: Wed, 30 Mar 2005 04:25:14 UTC
Posts: 12098
Location: Austin, TX
Ilaggoodly wrote:
seems like you don't really understand what you are doing since this is a relatively trivial problem. Also it is not "other topics in advanced mathematics" so I moved it to Computer science...

one example might be

Code:
factorial(n)
     if n<1
       return 0
     else
       fact := 1
       for i := 1-->n
          fact = fact*i
     return fact


What about 0! ?

Code:
factorial(n)
   if n<0
      return 0
   else...


I assume whatever code is being used knows empty products are 1 (empty when top limit < bottom limit), or you might need to fiddle and just define factorial(0) := 1, but anyways.

_________________
(\ /)
(O.o)
(> <)
This is Bunny. Copy Bunny into your signature to help him on his way to world domination


Top
 Profile  
 
 Post subject:
PostPosted: Tue, 1 Dec 2009 20:34:29 UTC 
Offline
Site Admin
User avatar

Joined: Thu, 15 Feb 2007 06:35:15 UTC
Posts: 755
odd as it is, the problem defined factorial (0) as 0.

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Wed, 2 Dec 2009 01:00:22 UTC 
Offline
Moderator
User avatar

Joined: Wed, 30 Mar 2005 04:25:14 UTC
Posts: 12098
Location: Austin, TX
Ilaggoodly wrote:
odd as it is, the problem defined factorial (0) as 0.


That is odd. Oh well, I rescind my objection, and thank you Ilaggoodly.

_________________
(\ /)
(O.o)
(> <)
This is Bunny. Copy Bunny into your signature to help him on his way to world domination


Top
 Profile  
 
 Post subject: Re:Write a function ‘factorial’ that takes in one input:
PostPosted: Thu, 21 Oct 2010 06:10:09 UTC 
Offline
S.O.S. Newbie

Joined: Thu, 21 Oct 2010 05:38:21 UTC
Posts: 3
Location: New york
there are three cases of inputs
1. If you will pass negative number then it will return zero,as no factorial for -ve no.
2. If you will zero,then it will return 1 as factorial zero is 1.
3. If you will pass any number greater then zero, then third return will execute and here recursion will
work. factorial function call recursively.

Ex .if you will pass 4 as a input
Then return factorial(3)*4 will call. Calling steps are

(factorial(3)*4) -------then function factorial will call 3 as a input
((factorial(2)*3)*4)-----then function factorial will call 2 as a input
(((factorial(1)*2)*3)*4)---then function factorial will call 1 as a input
((((factorial(0)*1)*2)*3)*4)----then function factorial will call 0 as a input

when factorial(0) will call thn it execute second return statement in function which will return 1
then calculation will perform like 1*1*2*3*4* =24
24 will be final return value


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Contact Us | S.O.S. Mathematics Homepage
Privacy Statement | Search the "old" CyberBoard

users online during the last hour
Powered by phpBB © 2001, 2005-2011 phpBB Group.
Copyright © 1999-2013 MathMedics, LLC. All rights reserved.
Math Medics, LLC. - P.O. Box 12395 - El Paso TX 79913 - USA