Python Forum
computing the factorial of N
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
computing the factorial of N
#3
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
Reply


Messages In This Thread
computing the factorial of N - by foolsgold27 - Jun-01-2017, 02:19 PM
RE: HELP SOLVING SIMPLE PYTHON PROBLEM - by Turry - Jun-01-2017, 03:54 PM
RE: HELP SOLVING SIMPLE PYTHON PROBLEM - by prog92 - Jun-01-2017, 05:56 PM
RE: HELP SOLVING SIMPLE PYTHON PROBLEM - by nilamo - Jun-01-2017, 07:06 PM
RE: HELP SOLVING SIMPLE PYTHON PROBLEM - by wavic - Jun-02-2017, 06:36 AM
RE: HELP SOLVING SIMPLE PYTHON PROBLEM - by wavic - Jun-02-2017, 08:27 AM
RE: computing the factorial of N - by martan45 - Aug-01-2017, 07:58 PM
RE: computing the factorial of N - by nilamo - Aug-01-2017, 08:08 PM
RE: computing the factorial of N - by martan45 - Aug-01-2017, 08:16 PM
RE: computing the factorial of N - by nilamo - Aug-01-2017, 08:30 PM
RE: computing the factorial of N - by ichabod801 - Aug-02-2017, 02:16 AM
RE: computing the factorial of N - by nilamo - Aug-02-2017, 02:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Program to Find the Factorial of a Number elisahill 2 1,573 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  computing average in nested loops cap510 5 5,396 Sep-11-2020, 12:33 PM
Last Post: deanhystad
  Fork the process (factorial) vndywarhol 3 3,512 Sep-07-2018, 03:28 AM
Last Post: ichabod801
  Computing average vestkok 2 2,600 Aug-12-2018, 10:02 AM
Last Post: vestkok
  Computing factorials Truman 6 4,289 Mar-14-2018, 06:38 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020