Python Forum

Full Version: computing the factorial of N
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Write A program for computing the factorial of N example (10!) where N! = (1*2*3*4*5*6*7*8*9*10),
(USING PYTHON)
FLOWCHART:
START
READ N
M=1, F=1
F=F*M
IS M=N?
IF YES, PRINT F -END
IF NO, M=M+1



PLS HELP ME Sick  Sick
How did you approach the problem and what exactly do you need us to help you with?
Write the code for you or what?
Your flow chart looks correct, though most factorial functions I've seen have started at N and worked down to 1, but this really only matters if you want to expand the program to tackle problems like dividing factorials.

Going through your flowchart:

READ N
Assuming you're using Python3 and are getting user input, this would be:
N = input("N: ")

M=1, F=1
Put those on separate lines, but otherwise that's exactly the right notation

F = F*M
Also correct notation, though you need to start a loop before this, since this step will be repeated for each value M

IS M = N?
IF YES, PRINT F -END
IF NO, M = M+1
This is a simple If statement. They use the following format:
if x:
   do this thing
else:
   do this other thing

In this case:
if M == N:
   print("Factorial = " + str(F))
   break loop (possibly by changing variable or break statement)
else:
   M += 1

The end code with the loop would be this:

N = input("N: ")

M = 1
F = 1

counting = True
while counting:
   F = F * M
   if M == N:
       print("Factorial = " + str(F))
       counting = False
   else:
       M += 1
Welcome to the forums!

We're here to help :)

...however, the emphasis is on "help". We won't do it for you, since that isn't helping. If you have some code, please share it, along with the output you're getting and the output you expect, along with any error messages, and we'll help you get back on track.
Use for loop
def fact(num):
    counter=1
    factVal=1
    print ('factorial = ', counter )
    while counter <= num:
        incr = counter
        if (counter !=1):
             print('*' , incr)
        factVal=factVal*counter
        counter=counter+1
    print(factVal)
fact(5)
Fix the indentation and use code tags, please!
I don't even bother to read code with no indentation.

Simple for loop will do the job.
This works for me

m = 1

answer = 1

n = int(input("What number to factorial? "))

while m <= n:
    answer = answer * m
    m = m + 1
print(answer)
This is two months old, so sure, let's throw answers around.

import functools

fact = int(input("What number to factorial? "))
print(functools.reduce(lambda x, y: x*y, range(1, fact+1)))
(Aug-01-2017, 08:08 PM)nilamo Wrote: [ -> ]This is two months old, so sure, let's throw answers around.

import functools

fact = int(input("What number to factorial? "))
print(functools.reduce(lambda x, y: x*y, range(1, fact+1)))

Although I have no idea what that means, it looks very cool and is probably the fastest way to find out the factorial of N Smile
Pages: 1 2