Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What is the error in this coding?

+2 votes
asked Sep 7, 2020 by Janani.T 12th-A1 (140 points)
num=int(input("Enter a number:"))
if(num==0):
    fact=1
    fact=1
    for i in range(1,num1+):
        fact=fact*i
        print("Factorial of",num,"is",fact)

5 Answers

+1 vote
answered Sep 9, 2020 by LiOS (6,420 points)
  • Logical error in 'if statement' since the code would only execute further if the number entered is 0. We would want a number 1 or greater (only if one if statement is present). In your case, you would want if(num != 0)
  • Double declaration and assignment of fact variable (not causes any issues but not needed)
  • In your for loop, 1 and + are wrong way round. We would want for..... range(1, num+1)
  • Alternative method of fact=fact*i is fact*=i
  • However, with the fixes, the factorial statement is printed multiple times and only the last statement printed is correct. To only print once, we would move the print statement to indented inline with for.

A working solution but slightly different to support negative numbers, 0 and 1 or greater:

num = int(input("Enter a number: "))

factorial = 1

if num < 0:
    print("Negative number don't have a factorial")
elif num == 0:
    print("The factorial of 0 is 1")
else:
    for i in range(1,num + 1):
        factorial = factorial*i
    print("The factorial of",num,"is",factorial)

+1 vote
answered Sep 25, 2020 by KV's Fun Time (520 points)
Hello.

This code will help you to solve your issue

number = int(input("Enter number: "))
f = number
factorial_value = 1
while number > 0:
    factorial_value *= number
    number -= 1
print("{}! = {}".format(f, factorial_value))

#f variable is used to format the ans value
#because in loop, the last value of number will be 0
#and 0! is 0 and not the ans that we get.
#factorial_value is set to 1 because if set to 0
#ans will always be 0.
#In the loop, number is multiplied with factorial value
#then number is decreased by 1
#and the loop will continue till number = 0 and
#as 0 = 0 and not greater than 0
#loop breaks and program prints the factorial value
+1 vote
answered Sep 26, 2020 by kk K (160 points)
for i in range(1,num1+):                                                                                                           
                          ^                                                                                                            
SyntaxError: invalid syntax

in for after num1 + should not come
–1 vote
answered Sep 28, 2020 by Jayden Neifert Reeves (530 points)

I don't do python really, but it looks like this code is incorrect.

0 votes
answered Oct 16, 2020 by Harshitha (200 points)

I think there are few errors in your code.

You just need to understand and correct few errors in your code.

num=int(input("Enter a number:"))
fact=1
if(num==0):
    fact=1
else:
    for i in range(1,num+1):
        fact=fact*i
print("Factorial of",num,"is",fact)

1.Before if statement, you have to declare a fact.

2.It is better to use if-else statement.
3.In for Statement,The range should be num+1.because the ending index is not considered in the loop.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...