fork download
  1. #!/bin/bash
  2. # your code goes here
  3. #!/bin/bash
  4.  
  5. echo "enter a number "
  6. read n
  7.  
  8. if [ "$n" -lt 0 ]; then
  9. echo "factorial is not defined for negative numbers"
  10. exit 5
  11. fi
  12.  
  13. fact=1
  14. i="$n"
  15.  
  16. while [ "$i" -gt 1 ]; do
  17. fact=$((fact * i))
  18. i=$((i - 2))
  19. done
  20.  
  21. echo "the factorial of the given number is $fact"
Success #stdin #stdout #stderr 0s 5320KB
stdin
Standard input is empty
stdout
enter a number 
the factorial of the given number is 1
stderr
./prog.sh: line 8: [: : integer expression expected
./prog.sh: line 16: [: : integer expression expected