Python Forum
computing the factorial of N
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
computing the factorial of N
#1
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
Reply
#2
How did you approach the problem and what exactly do you need us to help you with?
Write the code for you or what?
Reply
#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
#4
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.
Reply
#5
Use for loop
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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)
Reply
#7
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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)
Reply
#9
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)))
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Program to Find the Factorial of a Number elisahill 2 1,438 Nov-21-2022, 02:25 PM
Last Post: DeaD_EyE
  computing average in nested loops cap510 5 5,176 Sep-11-2020, 12:33 PM
Last Post: deanhystad
  Fork the process (factorial) vndywarhol 3 3,415 Sep-07-2018, 03:28 AM
Last Post: ichabod801
  Computing average vestkok 2 2,517 Aug-12-2018, 10:02 AM
Last Post: vestkok
  Computing factorials Truman 6 4,098 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