TCL Interview Programs
Disclaimer – There are always multiple approaches to solve
a problem. We have used only one out of those. You can try other logic based on
your understanding. Please comment your concern to us, we may help you in your
understanding on TCL Programmings.
1.
Write a program to print a string in TCL ?
Ans – Strings can be printed using double quotes,
single quote or curly braces. Please refer the below example for trying
dfferent variables.
set
a "TCL Programs"
puts
"$a"
Output : tclsh main.tcl
TCL Programs
2.
Write a program to accept two numbers from user and print
their sum?
Ans –
puts
"enter a number"
set
1stnum [gets stdin]
puts
"enter another number"
set
2ndnum [gets stdin]
set
result [expr $1stnum + $2ndnum]
puts
“Sum of both the numbers is $result”
Output : tclsh main.tcl
Enter a number
345
Enter another number
100
Sum of both the numbers is 445
3.
Write a program to accept two strings from user and print
their difference?
Ans -
puts
"enter a number"
gets
stdin fst
puts
"enter another number"
gets
stdin snd
set
c [expr $fst - $snd]
puts
"difference between both the numbers is $c"
Output : tclsh main.tcl
Enter a number
89
Enter another number
90
Difference between both the numbers
is -1
4.
Write a program to accept two strings from user and print
their product(multiply) and quotient(divide)?
Ans –
puts
"enter a number"
gets
stdin fst
puts
"enter another number"
gets
stdin snd
set
c [expr $fst * $snd]
set
d [expr $fst/$snd]
puts
"Product of both the numbers is $c"
puts
"Quotient of both the numbers is $d"
Output : tclsh main.tcl
Enter a number
10
Enter another number
10
Product of both the numbers is 100
Quotient of both the numbers is 1
5.
Write a program to print area of a circle. A(circle)= 3.142
* R * R ?
Ans –
puts
"enter radius"
gets
stdin R
set
AREA [expr 3.14 * $R * $R]
puts
"Area of the given circle with Radius- $R is : is $AREA"
Output : tclsh main.tcl
Enter radius
2
Area of the given circle with
Radius- 2 is : is 12.56
6.
Write a program to print area of a triangle A(Triangle)=
0.5 * B * H
Ans -
puts
"Enter Base"
gets
stdin B
puts
"Enter Height"
gets
stdin H
set
AREA [expr 0.5 * $B * $H]
puts
"Area of the given Triangle with Breadth- $B and Height- $H is :
$AREA"
Output : tclsh main.tcl
Enter Base
2
Enter Height
2
Area of the given triangle with Breadth- 2 and Height- 2 is
: 2.0
Write
a program to print simple interest SI = (PNR)/100
Ans –
puts
"Enter Principal"
gets
stdin P
puts
"Enter Rate"
gets
stdin R
puts
"Enter Time in months"
gets
stdin N
set
SI [expr ($P * $N * $R)/100]
puts
"SI with P- $P , R - $R , Time - $N is $SI"
Output : tclsh main.tcl
Enter Principal
100
Enter Rate
100
Enter Time in months
3
SI with P- 100 , R – 100 , Time – 3
is 300
Write a program to
accept two values AND swap them using 3rd variable
Ans –
puts
"Enter 1st number"
gets
stdin a
puts
"Enter 2nd number"
gets
stdin b
puts
"Before Swapping a= $a and b = $b"
set
temp $b
set
b $a
set
a $b
puts
"After Swapping a= $a and b = $b"
Output : tclsh main.tcl
Enter 1st number
1290
Enter 2nd number
9090
Before Swapping a= 1290 and b= 9090
After Swapping a= 9090 and b= 1290
Write a program
to accept roll and marks of 3 subjects of a student. Calculate total of 3
subjects and average.
Ans –
puts "Enter roll number"
gets
stdin roll
puts
"Enter marks 1"
gets
stdin m1
puts
"Enter marks 2"
gets
stdin m2
puts
"Enter marks 3"
gets
stdin m3
set
total [expr $m1 + $m2 + $m3]
set
average [expr $total/3]
puts
"Student Roll Number : $roll"
puts
"Phy Marks = $m1 , Math Marks = $m2 , Chem Marks = $m3"
puts
"Total Marks in all Subjects = $total"
puts
"Average Marks = $average"
Output : tclsh main.tcl
Enter roll number
122
Enter marks 1
90
Enter marks 2
90
Enter marks 3
90
Student Roll Number : 122
Phy Marks = 90, Math Marks = 90,
Chem Marks = 90
Total Marks in all Subjects = 270
Average Marks = 90
Print following
outputs : http:\\www.google.com using TCL
Ans –
Puts “http:\\\\google.com
#Please try this again to find the difference
Print largest
of 2 numbers
Ans –
puts
"Enter 1st number"
gets
stdin a
puts
"Enter 2nd number"
gets
stdin b
puts
"Numbers Entered are $a and $b"
if
{$a > $b} {
puts
"Largest number is $a"
}
else {
puts
"Largest number is $b"
}
Output: tclsh main.tcl
Enter 1st number
8989
Enter 2nd number
9
Numbers Entered are 8989 and 9
Largest number is 8989
Write a program
to find if positive or negative.
Ans –
puts
"Enter a number"
gets
stdin a
puts
"Number Entered is $a"
if
{$a > 0} {
puts
"$a is positive integer"
}
else {
puts
"$a is negative integer"
}
Output: tclsh main.tcl
Enter a number
90
Number Entered is 90
90 is positive integer
Write a program
to check if it is >10, <10 or equals 10.
Ans –
puts
"Enter a number"
gets
stdin a
puts
"Number Entered is $a"
if
{$a > 10} {
puts
"$a is greater than 10"
}
elseif {$a < 10} {
puts
"$a is less than 10"
}
else {
puts
"$a is equal to 10"
}
Output: tclsh main.tcl
Enter a number
90
Number Entered is 90
90 is greater than 10
Write a program
to find if its even or odd.
Ans –
puts "Hello World!"
puts
"Enter a number"
gets
stdin a
puts
"Number Entered is $a"
if
{$a%2 == 0} {
puts
"Number is even"
}
else {
puts
"Number is Odd"
}
Output : Enter a number
9
Number Entered is 9
Number is Odd
Write a program
to find numbers divisible by 5
Ans –
puts
"Enter a number"
gets
stdin a
puts
"Number Entered is $a"
if
{$a%5 == 0} {
puts
"Number is divisible by 5"
}
else {
puts
"Number is not divisible by 5"
}
Output :
Enter a number
90
Number Entered is 90
Number is divisible by 5
Write a program
to accept 2 numbers from user and compare them.
Ans –
puts
"Enter a number"
gets
stdin a
puts
"Enter another number"
gets
stdin b
puts
"Numbers Entered are $a and $b"
if
{$a > $b} {
puts
"a is greater than b"
}
elseif {$a<$b} {
puts
"b is greater than a"
}
else {
puts
"a and b are equal"
}
Output : tclsh main.tcl
Enter a number
89
Enter another number 9
Numbers Entered are 89 and 9
A is greater than b
Write a program
to accept 3 numbers and print in ascending Order.
Ans –
puts
"Enter 1st number "
gets
stdin a
puts
"Enter 2nd number"
gets
stdin b
puts
"Enter 3rd number"
gets
stdin c
if
{$a > $b && $a > $c && $b > $c} {
puts "Ascending Order is : $a $b $c"
}
else {
puts
"wrong"
}
....
Output : tclsh main.tcl
Enter 1st number
5
Enter 2nd number
4
Enter 3rd number
3
Ascending Order is: 5 4 3
Write a Program to
print numbers multiple of 4 between 1 to 20 using while loop
Ans –
set
i 1
while
{$i < 20} {
if
{$i%4==0} {
puts
$i
}
incr
i
}
Output :tclsh main.tcl
4
8
12
16
Write a Program to accept a number and print its
reverse using while loop
Ans -
puts
"enter a number"
gets
stdin n
while
{$n>0} {
set
rem [expr $n%10]
puts
$rem
set
n [expr $n/10]
}
Output : tclsh main.tcl
890
0
9
8
Write a Program to accept a number and print sum of
its digits
Ans –
puts "Enter a number "
gets stdin n
set sum 0
while {$n>0} {
set rem [expr $n%10]
set sum [expr $sum+$rem]
set n [expr $n/10]
}
puts "Sum of digits : $sum"
Output :
tclsh main.tcl
890
Sum of digits
: 17
Write a program to check if it is Armstrong number –
153=1^3 + 5^3 + 3^3
Ans -
set i 2
set sum 0
puts "enter a number"
gets stdin n
set temp $n
while {$n>0} {
set rem [expr $n%10]
set sum [expr $sum+($rem*$rem*$rem)]
set n [expr $n/10]
}
if {$temp==$sum} {
puts "Armstrong Number = $temp"
} else {
puts "Not Armstrong"
}
Output :
tclsh main.tcl
Enter a
number
153
Armstrong
Number = 153
Will keep updating this with more programs.....
ReplyDeleteThank you
ReplyDeleteThanks admin for sharing this information.
ReplyDeleteTCL Price in Pakistan