anu2468   -  Jun 05, 2014

Hi Team ,

I am new to TCL scripting . could you please tell me what is the mistake i have done in the below prog. I am trying to write program for base power such as 2 power 2 or 3 power 5 etc.

proc Factorial {x y} {
set i 1;
while {$i <= $x} {
set x [expr $x * $i]
incr i
}
return $x
}
puts " [Factorial 2 2]"

The output shows blank . Please assist me here.

Kylar  -  Jun 05, 2014

first excuse me for my bad english. your loop condition will never be true.
for fractorial i would write something like
proc factorial { n } {
if { $n >= 0 } {
set result 1
for { set i 1 } { $i <= $n } { incr i } {
set result [expr {$result*$i}]
}
return $result
}
return -code error "n can not be less than zero"
}
may you wanna read more about tcl an fractional (because of integer limit) so here is a link to the wiki http://wiki.tcl.tk/4520

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.