๐Ÿ“œ
B.TECH III YEAR โ€ข II SEMESTER โ€ข R22

Scripting Languages - Lab Manual

Scripting Languages Laboratory โ€ข Program 11 Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š II Semester ๐Ÿ“œ Scripting Languages ๐Ÿ’Ž Ruby โ€ข TCL โ€ข Perl ๐Ÿงช Lab Program
Aim of Program 11

Write a TCL script to find the factorial of a number.

โ† All Programs
Solution

Aim:

☛   Write a TCL script to find the factorial of a number?

Solution :

PROGRAM: (factorial.tcl)

 
puts "Enter a number:"
set num [gets stdin]

set fact 1

for {set i 1} {$i <= $num} {incr i} {
    set fact [expr {$fact * $i}]
}

puts "Factorial of $num is $fact"

puts "Total Marks: #{total_marks}"



OUTPUT:

 
Enter a number:
5
Factorial of 5 is 120



(OR)

PROGRAM: (factorial_recur.tcl)

 
proc factorial {n} {
    if {$n <= 1} {
        return 1
    } else {
        return [expr {$n * [factorial [expr {$n - 1}]]}]
    }
}

puts "Enter a number:"
set num [gets stdin]

puts "Factorial of $num is [factorial $num]"




OUTPUT:

 
Enter a number:
5
Factorial of 5 is 120



โ† Back to Scripting Languages Lab Programs ๐Ÿ“š All Lab Manuals