What is the error in this coding?

+1 vote
asked Sep 7 by Janani.T 12th-A1 (130 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

0 votes
answered Sep 9 by LiOS (4,660 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)

–2 votes
answered Sep 19 by Arti Ahirwar (200 points) 2 flags
  • 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)

0 votes
answered 3 days ago by KV's Fun Time (500 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
0 votes
answered 3 days ago by kk K (140 points)
for i in range(1,num1+):                                                                                                           
                          ^                                                                                                            
SyntaxError: invalid syntax

in for after num1 + should not come
0 votes
answered 1 day ago by Jayden Neifert Reeves (500 points)

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

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.
...