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

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Factorial FunctionPosted: Tue, 1 Dec 2009 10:27:34 UTC
 S.O.S. Newbie

Joined: Tue, 1 Dec 2009 10:13:56 UTC
Posts: 2

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

 Post subject: Posted: Tue, 1 Dec 2009 11:17:11 UTC

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

_________________

Top

 Post subject: Posted: Tue, 1 Dec 2009 19:55:07 UTC
 Moderator

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

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

 Post subject: Posted: Tue, 1 Dec 2009 20:34:29 UTC

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

_________________

Top

 Post subject: Posted: Wed, 2 Dec 2009 01:00:22 UTC
 Moderator

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

 Post subject: Re:Write a function â€˜factorialâ€™ that takes in one input:Posted: Thu, 21 Oct 2010 06:10:09 UTC
 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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 6 posts ]

 All times are UTC [ DST ]

#### Who is online

Users browsing this forum: No registered users

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

Search for:
 Jump to:  Select a forum ------------------ High School and College Mathematics    Algebra    Geometry and Trigonometry    Calculus    Matrix Algebra    Differential Equations    Probability and Statistics    Proposed Problems Applications    Physics, Chemistry, Engineering, etc.    Computer Science    Math for Business and Economics Advanced Mathematics    Foundations    Algebra and Number Theory    Analysis and Topology    Applied Mathematics    Other Topics in Advanced Mathematics Other Topics    Administrator Announcements    Comments and Suggestions for S.O.S. Math    Posting Math Formulas with LaTeX    Miscellaneous