All I want to do is Multiply...

All I want to do is find 5!.

read num
num={1..5}  
while [ "$num" -le 5 ]
do
f=[ "$num" * "$num" ]
done
echo f

Error Msg.

line 5: ${1..5} bad substitution
line 6: integer expression expected

Line 5 is the num=...
Line 6 is the "while" statement

I am new at this, and I am really, really trying.
Please help:confused:

Something like this?

f=1

for num in {1..5}
do
  f=$(( $f * $num ))
done

echo $f

Thank you so much Franklin52.
I got my expected results after numerous syntax errors. :slight_smile:
I had

do f=$(( "$f" * "$num" ))
done
echo "$f"

Bash did not like all of my quotes. I thought I had to use double quotes to reference variables.

This was the syntax error:

line 8: "1" * "1": syntax error: operand expected (error token is " "1" *  "1" ")

Any idea why sh did not like my quotes.

I got rid of the quotes an the script works fine.:slight_smile:
Ccccc
Thanks again.

Dear Friend,

You can use the following coding also.

i=1

for fact in `seq 1 5`
do
          i=$(( $i * $fact ))
done

  echo $i

Using a Perl script you can achieve the requirement as follows

my $i = 1;
my $result = 1;
$result *= $i++ while ($i <= 5) ;
print "Value : $result\n";

Franklin52 code is working fine.If it isn't working to you(Ccccc),then try the following code.

f=1

for fact in {1..5}
do
      let f*=fact
done

echo "Factorial of 5 is:$f"

Many, many thanks to Franklin52, Murugaperumal, thillai_selvan, & Vivekraj. Franklin52's code is working great for me, and the other code is enlarging my shell knowledge base.

Does anyone know how I can code this for any number, x!?

Ex. "please enter a number: "
read some number

I don't know how to code the "some number" multiplication part.
Thank again,
Ccccc

Franklin52 solution adapted for your latest requirement.

#!/bin/bash
f=1
echo -n "Enter number : "
read num
var=1
while (( $var <= $num ))
do
  f=$(( $f * $var ))
  let var++
done

echo $f

Thank you, Danmero. That's what I wanted.

Question concerning

 
let var++

I understand that "let" stores values in "var", and I theoritically understand that "++" increments the var values, but I don't really understand "++", or for that matter "--".
I know they are C operators, but I can't find any good explanations.
Is "++" saying "when you see me increase me by 1"??
Thanks,
Ccccc

increment(++) or decrement(--) value of var by 1(only).
If you want to increment/decrement the value of var by a different value you can use something like

i=0
while [ $i -lt 100 ]
do 
    let i+=10
    echo $i
done

You can also use recursion to find the factorial of a number. In the following example the function factorial() calls itself repeatedly until it's argument is 1.

#!/bin/bash

factorial()
{
  if (( $1 > 1 )); then
    i=$(( $1 - 1 ))
    j=$(factorial $i)
    echo $(( $1 * $j ))
  else
    echo 1
  fi
}

while :
do
  read -p "Enter a number: " x
  factorial $x
done

Try the following script
It will prompt the user to enter a number and perform the stuff

use strict;
use warnings;

print "Enter a number: ";
my $n = <>;#getting from stdin
my $i = 1;
my $res = 1;
$res *= $i++ while ($i <= $n) ;
print "Value: $res\n";

Sample output

Enter a number: 7
Value : 5040

You can find the factorial of any number by following way.

f=1
echo "Enter a number"
read no
if [[ $no -le 1 ]];then
echo "Factorial of $no is:1"
else
#for fact in `seq $no`
for((fact=1;fact<=$no;fact++))
do
      let f*=fact
done
echo "Factorial of $no is:$f"
fi
echo "Enter a number";
read num;
i=1

for fact in `seq 1 $num`
do
                  i=$(( $i * $fact ))
          done

            echo $i

Many, many Thanks to all of you.