![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with recursion in subdirectories | scotty_123 | Shell Programming and Scripting | 5 | 03-11-2007 05:51 AM |
| A Question On Recursion In Ksh | marlonus999 | Shell Programming and Scripting | 1 | 01-11-2007 07:09 AM |
| allow recursion on dns server? | xnightcrawl | UNIX for Advanced & Expert Users | 1 | 03-29-2006 10:36 AM |
| recursion too deep | swamy455 | Shell Programming and Scripting | 3 | 07-18-2005 03:18 PM |
| recursion | gsjf | Shell Programming and Scripting | 1 | 08-26-2002 12:22 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
factorial using recursive function?
Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me
factorial() { if [ $1 -gt 1 ] then y=`expr $1 - 1` x=$(( $1 \* factorial $y )) return $x else return 1 fi } echo -n "Enter number = "; read n factorial $n |
|
||||
|
Help Help Help in recursion
Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me
factorial() { if [ $1 -gt 1 ] then y=`expr $1 - 1` x=$(( $1 \* factorial $y )) return $x else return 1 fi } echo -n "Enter number = "; read n factorial $n |
|
||||
|
HI,
I have made a small change in your code and it is working fine for me...try it... factorial() { if [ $1 -gt 1 ] then y=`expr $1 - 1` factorial $y #x=$(( $1 \* factorial $y )) -- I have commented this x=$(( $1 * $? )) return $x else return 1 fi } echo -n "Enter number = "; read n factorial $n echo $? Thanks Raghuram |
|
||||
|
No support for Recusrsive functions in Shell scripting
Hello,
As far as i know, recursive functions are not supported in Shell Scripting. Anyways, the following script will help you to get the factorial of a number. #! /bin/sh echo "Enter a number: " read num i=2 res=1 if [ $num -ge 2 ] then while [ $i -le $num ] do res=`expr $res \* $i` i=`expr $i + 1` done fi echo "Factorial of $num = $res" |
|
|||||
|
Please review our rules and note:
(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post or send a private message where your goal is to get an answer more quickly. I have merged your two different threads. Please do not do this again. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|