SERVFORU

Tcl and OTcl Tutorial for NS2 - Program to find Factorial



 Fractorial Computation: tcl script to obtain the value of 10! = 10 * 9 * ... * 1.

  1. get factorial.tcl and run the script;
  2. write a function to compute 2^x, test your answer.
Execute the script as:

$ tclsh lab1a.tcl
or
$ ns lab1a.tcl



################################################################################


#filename : factorial.tcl
# define function to compute Factorial X!
proc Factorial {x} {
    # define variable
    set result 1
   
    # for loop
    for {set i 1} {$i <= $x} {incr i} {
set result [expr $result * $i]
    }
   
    # return computation result
    return $result
}


#############################################################################


# define function to compute 2^x
proc 2pow {x} {
    # define variable
    set result 1
   
    # for loop
    for {set i 1} {$i <= $x} {incr i} {
# fill in here
    }
   
    # return computation result
    return $result
}
# make function call
set result [Factorial 10]

# output result
puts "$result"

# make function call
set result [2pow 10]

# output result
puts "$result"
Share this article :
 

Post a Comment

 
Support : Ebin EPhrem | Ebin Ephrem | #Gabbarism
Copyright © 2011. Services | Embedded Support | Reviews | Virtual Technologys - All Rights Reserved
Template Created by ebinephrem.com Published by Ebin Ephrem
Proudly powered by Blogger