Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Experiment1 : To implement a program that performs basic arithmetic operations

Objective: TO develop an understanding about the basic syntax used in TCL language on NS2

# basic syntax used in TCL language on NS2


set a 45
set b 15
set c [expr $a + $b]
puts " sum is"
puts $c

set d [expr $a * $b]


puts "product is"
puts $d

set e [expr $a - $b]


puts "subtraction is "
puts $e

set f [expr $a / $b]


puts "quotient is"
puts $f
Experiment2 : To implement a program that performs sum of first 10 natural numbers.
Objective: To develop an understanding about the basic syntax by printing the sum of first 10
natural numbers in TCL language on NS2

# To print sum of first 10 natural numbers


set sum 0
for { set i 1 } { $i <=10 } { incr i} {
set sum [expr $sum + $i]
}
puts "sum is"
puts $sum
# print factorial of 10
set multiply 1
for { set i 1 } { $i <= 10} { incr i } {
set multiply [expr $multiply * $i]
}

puts "factorial 10 is "


puts $multiply
# To print first 20 prime numbers
for { set i 1} { $i <= 20 } { incr i} {
set flag 0
for { set j 2} { $j <= $i/2} { incr j} {
if { $i % $j == 0} {
set flag 1
break
}
}
if { $flag == 0 } {
puts $i
}
}
# to sort three numbers
set a 443
set b 891
set c 143

if { $a < $b } {
if { $a < $c } {
puts $a
if { $b < $c } {
puts $b
} else{
puts $c
}
}
if { $c < $b } {
puts $c
puts $a
puts $b
}
} else {
if { $b < $c } {
puts $b
if { $a < $c } {
puts $a
puts $c
} else {
puts $c
puts $a
}
} else {
puts $c
puts $b
puts $a
}
}

You might also like