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