Knowee
Questions
Features
Study Tools

Write a Shell script to find Factorial of a given number.

Question

Write a Shell script to find Factorial of a given number.

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure, here is a simple shell script to calculate the factorial of a given number:

#!/bin/bash

# Function to calculate factorial
factorial() {
    if [ $1 -eq 0 ]
    then
        echo 1
    else
        echo $(( $1 * $(factorial $(( $1 - 1 ))) ))
    fi
}

# Read the number
echo "Enter a number: "
read num

# Call the factorial function
result=$(factorial $num)

# Print the result
echo "The factorial of $num is: $result"

Here's how it works:

  1. The script starts with #!/bin/bash which tells the system that this is a Bash script.
  2. We define a function factorial that takes one argument, the number for which we want to calculate the factorial.
  3. Inside the function, we use an if-else statement to check if the number is 0. If it is, we return 1 because the factorial of 0 is 1.
  4. If the number is not 0, we recursively call the factorial function with the argument decremented by 1, and multiply the result with the current number.
  5. We read the number from the user using the read command and call the factorial function with this number.
  6. Finally, we print the result.

This problem has been solved

Solution 2

Sure, here is a step-by-step guide to write a shell script to find the factorial of a given number:

  1. Open a text editor and create a new file. Give it a suitable name, such as "factorial.sh".

  2. Start the script by adding the shebang line at the top of the file. This line tells the system which interpreter to use to run the script. In this case, we will use the bash shell. So, add the following line at the beginning of the script:

    #!/bin/bash
    
  3. Declare a variable to store the factorial value. Let's call it "factorial". Initialize it to 1. Add the following line to the script:

    factorial=1
    
  4. Prompt the user to enter a number for which they want to find the factorial. You can use the "read" command to read the input from the user. Add the following line to the script:

    echo "Enter a number:"
    read number
    
  5. Use a loop to calculate the factorial. We will use a for loop that iterates from 1 to the given number. Inside the loop, multiply the current value of the factorial variable with the loop variable. Add the following lines to the script:

    for (( i=1; i<=number; i++ ))
    do
        factorial=$((factorial * i))
    done
    
  6. Finally, display the factorial value to the user. Add the following line to the script:

    echo "The factorial of $number is $factorial"
    
  7. Save the file and exit the text editor.

  8. Make the script executable by running the following command in the terminal:

    chmod +x factorial.sh
    
  9. Now, you can run the script by executing the following command:

    ./factorial.sh
    
  10. The script will prompt you to enter a number. After entering the number, it will calculate and display the factorial.

That's it! You have successfully written a shell script to find the factorial of a given number.

This problem has been solved

Similar Questions

Write a python function to find the factorial of given number

Write a program to calculate the factorial of N.

Python Program to Find the Factorial of a Number Without Recursion

write a recursive function to find factorial of a number.

00:47Write a program to find a factorial of a number

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.