get positive number n as argument script must calculate the factorial of its argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get positive number n as argument script must calculate the factorial of its argument
# 1  
Old 04-02-2009
get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script?

I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1.

Here is a start but I have no clue how to continue:
Code:
#!/bin/sh

Thanks in advance...
# 2  
Old 04-02-2009
Code:
## Set $n to the factorial number
## Use the first command-line argument if set, otherwise
## default to 5
n=${1:-5}

## Loop while $n is greater than 1
while [ $n -gt 1 ] 
do

  ## $f is the factorial in progress
  ## if not set, it is initialized with 1
  ## It should be set at the top of the script
  ## in case of existing value
  ##
  ## Multiple $f by $n
  f=$(( $n * ${f:-1} ))

  ## Decrement $n
  n=$(( $n - 1 ))
done

## Print the result
echo $f


Last edited by cfajohnson; 04-06-2009 at 10:25 PM..
# 3  
Old 04-06-2009
Hi,

Thanks for this solution ...
Can you please put in with comments what does what?

Thanks,
# 4  
Old 04-28-2009
thanks for the explanation cfajohnson
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting number of argument passed to a shell script

Hi Experts, I have been trying to work on a simple shell script that will just add the two argument passed to it. Here is what i tried : #!/bin/bash welcome(){ echo "Welcome to this Progg. which will accept two parameter" } main_logic(){ arg=$# echo "Number of argument passed is... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

2. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

3. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

4. Shell Programming and Scripting

how to specify number of argument in shell

Hi I am new in shell, I am trying to create a small script that can do exit if a script is executed when argument not 2 #!/bin/sh if ; then echo greater exit 1; elif ; then echo less exit 1; fiit keeps returning me whatever number of argument I... (1 Reply)
Discussion started by: peuceul
1 Replies

5. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

6. UNIX for Dummies Questions & Answers

How to print number before argument?

Hey everyone, I need to write a script that will display the number of the argument before displaying the argument itself. Here's what I have so far: for arg in "$@" do echo $#:$arg done This returns the sum of all arguments preceding the arguments themselves. $(script) a b c 3:a... (1 Reply)
Discussion started by: unclepickle1
1 Replies

7. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

8. UNIX for Dummies Questions & Answers

Script with an argument

I have a script named abc.sh and another script ab.sh I execute one as: ./abc.sh and another ./ab start that is with an argument , so wanted to know what is this start a file or something else? (4 Replies)
Discussion started by: nixhead
4 Replies

9. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

10. Shell Programming and Scripting

port number as an argument

What is the script that takes a port number as parameter and displays status, whether it is available or is already used by other process pls help (1 Reply)
Discussion started by: saikiran
1 Replies
Login or Register to Ask a Question