Sunday, 12 June 2016

TCl Basic Programs

Basic TCL Programs:
==================

1.TCL program to find the given numeric value is positive or negative using string commands

    set a -10
    if {$a < 0} {
       puts "$a Is a Negative Number"
    } else {
       puts "$a Is a Positive Number"
     }


2.TCL Program to find maximum of 3 given numbers.


set a 100
set b 200
set c 150
if {$a>$b && $a>$c} {
puts “a is bigger and value is $a” } elseif {$b>$a && $b>$c} {
puts “b is bigger and value is $b” } else {
puts “c is bigger and value is $c” }


3. TCL program to find number of vowels in a string using string commands


set str "abcde"
set c 0
set l [string length $str]
puts " Length Of The String = $l"
for {set j 0} {$j<$l} {incr j} {
set b [string index $str $j]
if { $b=="a" | $b=="e" | $b=="i" | $b=="o" | $b=="u"} {incr c}
}
puts " The Number Of vowels Are =$c"


4.TCL program to find out whether the string given by user is palindrome.


set a madam
set len [ string length $a ]
set n [ expr $len/2 ]
for { set i 0 } { $i < $n } { incr i 1 } {
set b [ string index $a $i ]
set c [ expr $len - 1 - $i ]
set d [ string index $a $c ]
if {$b != $d} {
puts "$a is not a Palindrome"
exit
        }
     }
puts "$a is a Palindrome"


5. Write a program that computes the sum of the numbers

   set num 100
   set sum 0
   for {set i 1} {$i<=$num} {incr i} {
   set sum [expr $sum + $i]
    }
   puts "The Sum of number $num is $sum"


6. Write a tcl program to convert decimal to binary


set i 1000
set res ""
while {$i>0} {
set as [expr {$i%2}]$as
        set i [expr {$i/2}]
}
if {$as==""} {
set as 0
}
split $as ""
puts "Binary of decimal number $i is: $as"


7 .Write a tcl program to convert hexadecimal to decimal.


set num 589
    set x [expr 0x$num]
puts "Decimal format = $x"


8.TCL program to swap two numbers .
x = x+y
y = x-y
x = x-y


set a 50
set b 60
set a [expr $a + $b]
set b [expr $a - $b]
puts "b = $b"
set a [expr $a - $b]
puts "a = $a"


9.TCL program to multiply two numbers without using multiplication operator.


set a 5; set b 10; set s 0
for {set i 1} {$i <= $b} {incr i} {
set s [expr $a + $s]
}
puts $s

10. TCL program to get name as input from user. Reverse the name [Using for loop] and print character by character [Using while loop].If you find a space or digit while printing, exit the program at that instance.

#! /usr/bin/tclsh
#input from user
puts "enter a number....."
gets stdin a
set len [string length $a]

for {set i $len} {$i >= 0} {incr i -1} {
set x [string index $a $i]
append b $x
}
puts "$b ...."

set i "0"
while {$i < $len} {
set x [string index $b $i]
if {$x == " "|| [string is digit $x]} {
puts "--->exit due to digit or space"
break
}
puts -nonewline $x
incr i;
}



11. TCL program to  find out the square root of given input


 
set num 256
if {$num < 0} {
puts "Number Entered: $number is -ve number"
} elseif {$num == 0 || $num == 1} {
puts "Square root of $num: is $num"
} else {
set i 1
set s_root 0
while {$num > 0} {
set num [expr $num - $i]
incr i 2
if {$num >= 0} {
incr s_root }
      }
puts "Square Root is: $s_root"
}


12. TCL program to prints the sum of two squares.

set num1 5; set num2 9;
set num1_1 [expr $num1*$num1]
set num2_2 [expr $num2*$num2]
puts "The Sum is:"
set result [expr $num1_1+$num2_2]
puts $result



13.TCL program to reverse a string.


set a manish
set b [string length $a]
puts $b
for {set i $b} {$i >= 0} {incr i -1} {
puts -nonewline [string index $a [expr $i -1]]
}


14. TCl Program to print fabonacci series.

set f1 0
set f2 1
set fibo ""
for {set i 0} {$i < 15} {incr i} {
    set f3 [expr {$f1 + $f2}]
    set f1 $f2
    set f2 $f3
    append fibo "$f1,"
}
puts "Fibonacci Series is $fibo"


15.TCl program to print Factorial of a number:

set fact 1
for {set i 0} {$i <= 10} {incr i} {
    puts "$i! = $fact"
    set fact [expr {$fact * ($i + 1)}]
}

16.How to verify whether system installed with 32 bit or 64 bit TCL library

% parray tcl_platform
tcl_platform(byteOrder) = littleEndian
tcl_platform(machine)   = i686
tcl_platform(os)        = Linux
tcl_platform(osVersion) = 2.6.32-220.el6.i686
tcl_platform(platform)  = unix
tcl_platform(tip,268)   = 1
tcl_platform(tip,280)   = 1
tcl_platform(user)      = manish
tcl_platform(wordSize)  = 4
%

If ->tcl_platform(wordSize) = 4 -> It is a 32 bit TCL.
If ->tcl_platform(wordSize) = 8 -> It is a 64 bit TCL.

2 comments:

  1. Great insights. I look forward to reading what you're planning on next, because your post is a nice read.

    kissanime alternatives

    ReplyDelete

Note: only a member of this blog may post a comment.