while num > 1:
    factorial = factorial * num
    num = num - 1
Answer from John Gordon on Stack Overflow
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ factorial with a while loop in python
Factorial with a While Loop in Python - codingem.com
April 10, 2023 - To find factorial using a while loop in Python, multiply the result (starting at 1) by the number - 1 in a loop until the number reaches 1.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to calculate factorial using while loop?
r/learnpython on Reddit: How to calculate factorial using while loop?
March 18, 2020 -

Factorial if you don't know is for ex. Say 5!=54321=120 I wrote this code:

n=1 k=input("Enter factorial here: ")

While n>=k:

print(n)

n=n+1

........................ Output comes say if k=3

3

2

1

I want to know How can I get 321 using while loop.

๐ŸŒ
The Crazy Programmer
thecrazyprogrammer.com โ€บ home โ€บ factorial program in python using for and while loop
Factorial Program in Python using For and While Loop
April 12, 2023 - Thanks for your help in advance! ... number = input (โ€œgive me a number =โ€) number = int (number) fac = 1 i = 1 print (fโ€ {number}! =โ€, end = โ€ โ€œ) while i <= number : fac = fac * i i = i + 1 if (i == number+1): print (i โ€“ 1 , end = "=") else : print (i โ€“ 1 , end = "X")
๐ŸŒ
Newtum
blog.newtum.com โ€บ factorial-of-a-number-in-python-using-while-loop
Factorial of a Number in Python Using While Loop - Newtum
October 15, 2024 - After we assign the desired value to the variable โ€˜nโ€™, we will denote the factorial variable as 1. This is vital because we are going to multiply โ€˜nโ€™ by the value of โ€˜factorialโ€™. ... We then enter the โ€˜whileโ€™ loop that continues as long as โ€˜nโ€™ is greater than 1. We want to repeat the multiplication until we reach n=1 ยท In the loop, we are going to multiply the current value of โ€˜fโ€™ variable by โ€˜nโ€™ variable that is f*n mathematically, which can be written as โ€˜*=โ€™ operator in Python.
๐ŸŒ
Vultr
docs.vultr.com โ€บ python โ€บ examples โ€บ find-the-factorial-of-a-number
Python Program to Find the Factorial of a Number | Vultr Docs
December 27, 2024 - def factorial_while(n): result = 1 number = 1 while number <= n: result *= number number += 1 return result Explain Code ยท Here, the while loop continues running as long as number is less than or equal to n.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ for/while loop and getting factorial value
r/learnpython on Reddit: For/while loop and getting factorial value
November 26, 2019 -

So I took a beginner Python class and I'm already lost.

We are supposed to build a calculator using for/while -loop. The program is supposed to ask a number and if the choice is "0" - the program will close. If the input is "1", the program will ask another number and will then get the factorial value of that number.

I can barely understand what I'm supposed to be doing and I got my code working as long as the input value is 0-3. After that it doesn't work. This is the given example:

  • 0! = 1

  • 1! = 0!*1 = 1

  • 2! = 1!*2 = 2

  • 3! = 2!*3 = 6

  • 4! = 3!*4 = 24

  • 5! = 4!*5 = 120

  • 6! = 5!*6 = 720

Edit:

Did all the changes and the code looks like this now! Still not passable tho. D:

Here are the requirements in case I have misunderstood something:

"You need to have variables "factorial" and " counter" they both have int values of 1!"

"While -loop must repeat until counter is less than or equal to num"

"You need to assign a variable called "factorial2" from factorial * counter"

"counter value +1 after every loop"

choice = input('0 - closes the program, 1 - get factorial value: ')

while True:
    
    if choice == '1':
        num = input('Give a number: ')
        num = int(num)
        factorial = 1
        
        for i in range(1, num + 1):
            factorial = factorial * i

        print(str(num) + '! = ',  factorial)
        
    elif choice == '0':
        print('Program will be closed!')
        break
    
    else:
        print('Please give either 0 or 1!')
        choice = input('0 - closes the program, 1 - get factorial value: ')
        continue

I'm so lost. This is literally my third assignment and all our materials for this assignment teach us is the basic functions of while/for -loop. :( Feeling so disheartened at the moment.

EDIT: Made some progress. It's still not passable but it's closer. I got a tip that I need to assign factorial2 variable from factorial * counter But... I don't get how can I do this. This code does what is asked, but it's not done the right way I guess...

๐ŸŒ
Rationalstudy
rationalstudy.com
Calculate Factorial using While Loop in Python
That is done by using If-Else block (as show below), in "if" part "n<0" condition is set, inside "else" part further coding for calculating factorial is required. if n<0: print("ERROR: Number entered is a negative integer") else: Before diving into the "while" loop part where we would be calculating factorial, another validation is required, if number entered is zero, then factorial of zero is 1, so for that part we need another nested if-else inside the above "else" block.
Find elsewhere
๐ŸŒ
SLU
cs.slu.edu โ€บ ~fritts โ€บ CSCI140_F12 โ€บ assignments โ€บ fact_procedure.html
Python procedure for Factorial (using While loop)
#------------------------------------------------ # Factorial function: # Input: n # Output: n! # def factorial (n): if (n <= 1): return 1 i = 1 product = 1 while (i <= n): product = product * i i = i + 1 return product #------------------------------------------------ # Main program: # (execution starts at the first statement below) # get_number = 1 while (get_number == 1): x = input ("Enter a number:") if (x < 0): print "Invald number" else: get_number = 0 print "The factorial of", x, "is", factorial(x)
๐ŸŒ
YouTube
youtube.com โ€บ education examination
Factorial Program in Python with For Loops and While Loops - YouTube
This video discusses how to construct a factorial program / algorithm in Python by using a for loop or a while loop. I also discuss the basic logic behind co...
Published ย  October 19, 2020
Views ย  1K
๐ŸŒ
Coding Connect
codingconnect.net โ€บ home โ€บ python program to find factorial of a number using while loop
Python Program to Find Factorial of a Number Using While Loop
January 2, 2023 - # Python program to find the factorial ... does not exist for negative numbers") else: while i <= num: fact = fact*i i = i + 1 print("The factorial of", num, "is", fact)...
๐ŸŒ
Quora
quora.com โ€บ What-is-a-program-to-find-factorial-using-a-while-loop-in-Python
What is a program to find factorial using a while loop in Python? - Quora
Answer (1 of 4): You can follow a typical accumulator pattern. If you donโ€™t know what that is you can Google it. In broad strokes the pattern is: 1. Initialize accumulator variable(s) 2. Loop until the result is achieved updating accumulators at each step Given that n hold the value of the fact...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-for-factorial-of-a-number
Factorial of a Number - Python - GeeksforGeeks
This method calculates factorial by manually multiplying the numbers from 1 to n inside a for loop. ... Produces the factorial after the loop completes.
Published ย  November 29, 2025
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-program-to-find-factorial-of-a-number
Python Program to find Factorial of a Number
March 31, 2025 - number = int(input(" Please enter any Number : ")) fact = 1 for i in range(1, number + 1): fact = fact * i print("The factorial of %d = %d" %(number, fact)) The user entered integer in the above program example is 4. Please refer to math functions, ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ factorial
Python Program to Find the Factorial of a Number
If the number is positive, we use for loop and range() function to calculate the factorial. # Python program to find the factorial of a number provided by the user # using recursion def factorial(x): """This is a recursive function to find the factorial of an integer""" if x == 1 or x == 0: ...
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ factorial program in python | examples & complexity analysis
Factorial Program In Python | Examples & Complexity Analysis
February 4, 2025 - Using a while loop, we repeatedly multiply the result by the counter variable and then increment the counter until it exceeds n. The example Python program below showcases this approach.
๐ŸŒ
AlmaBetter
almabetter.com โ€บ bytes โ€บ articles โ€บ factorial-program-in-python
Factorial Program in Python(Factorial of a Number in Python)
October 19, 2024 - Here's a Python program to achieve that: ... The factorial_while_loop function uses a while loop that continues to multiply the current value of n and decrements n in each iteration until it reaches 0.
๐ŸŒ
Brainly
brainly.in โ€บ computer science โ€บ secondary school
. write a python program to find the factorial of given number using while loop - Brainly.in
February 1, 2023 - The function checks if n is equal to 0, and if it is, returns 1. If n is not equal to 0, the function initializes a variable fact to 1 and then uses a while loop to multiply fact by each integer from n down to 1, until n becomes 0.
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ factorial program in python: explained with examples
Factorial Program in Python: Explained with Examples
November 9, 2023 - Learn to code a factorial program in Python with our easy guide. Master loops & recursion techniques for your coding toolbox. Continue reading!
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
Medium
medium.com โ€บ @harshitbasti โ€บ find-factorial-in-python-ddc95712a3a7
Find Factorial in Python. In this article I will discuss how toโ€ฆ | by Harshit Singh | Medium
January 16, 2025 - #code 1 number = int(input("Enter a number :")) factorial =1 if number < 0: print("Factorial is not defined for negative numbers.") else: if number == 0: print(factorial) else: while number >=1: factorial= factorial*number number-=1 print(factorial) ...