Write a Shell script to find Factorial of a given number.
Question
Write a Shell script to find Factorial of a given number.
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:
- The script starts with
#!/bin/bashwhich tells the system that this is a Bash script. - We define a function
factorialthat takes one argument, the number for which we want to calculate the factorial. - 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.
- If the number is not 0, we recursively call the
factorialfunction with the argument decremented by 1, and multiply the result with the current number. - We read the number from the user using the
readcommand and call thefactorialfunction with this number. - Finally, we print the result.
Solution 2
Sure, here is a step-by-step guide to write a shell script to find the factorial of a given number:
-
Open a text editor and create a new file. Give it a suitable name, such as "factorial.sh".
-
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 -
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 -
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 -
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 -
Finally, display the factorial value to the user. Add the following line to the script:
echo "The factorial of $number is $factorial" -
Save the file and exit the text editor.
-
Make the script executable by running the following command in the terminal:
chmod +x factorial.sh -
Now, you can run the script by executing the following command:
./factorial.sh -
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.
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
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.