FAQ  •  Login

finding factorial of asum of two numbers

Moderators: UncleTimmy, mjpieters

Enjoying the challenge? Need a hint?
Make a donation and help keep the site running! -thesamet
<<

sumit kumar

Posts: 4

Joined: Tue Jan 10, 2012 12:44 am

Post Thu Jan 12, 2012 3:14 am

finding factorial of asum of two numbers

  Code:
    def add(a,b):
      c=a+b
      print c
      x=c
      def factorial(x):
       fact=1
       while x>0:
        fact=fact*x
        x=x-1
      return fact



here is my code and i am confused.
actually first i want to add two numbers and then i want to find factorial of sum of two numbers.but i m not getting the result.


Any ideas why?
Help would be greatly appreciated.
<<

blast_hardcheese

Posts: 32

Joined: Wed Aug 17, 2011 12:52 am

Post Fri Jan 13, 2012 6:49 pm

Re: finding factorial of asum of two numbers

sumit kumar wrote:actually first i want to add two numbers and then i want to find factorial of sum of two numbers.


Probably better to break these up into component pieces:

  Code:
def add(a,b):
    c = a + b
    return c

def factorial(x):
    fact=1
    while x>0:
        fact=fact*x
    return fact


Once we do that, we can check to see if the functions actually do what they should do:

  Code:
print "Add:", add(1,2)
print "Fact:", factorial(3)


We see the "3" is printed from add, but we never see the factorial function finish. This is because of an infinite loop in factorial(), see if you can find it :)
If you can't, I'll point it out

Return to Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.