12. Write a tcl program ro print numbers from 1 to 50 in ascending and descending order using for loop.
Answers
Answered by
6
The given problem is solved using language - TCL.
1. To print 1 - 50.
for {set i 1} {$i < 51} {incr i} {
puts $i
}
2. To print 50 - 1.
for {set i 50} {$i > 0} {incr i -1} {
puts $i
}
#1
- At first, we have created a variable named 'i' in the for loop with value = 1.
- Then a condition is set [$i < 51] to display the numbers from 1 to 50.
- Then we will increment the value of 'i'. When no value is given, the default value is taken as 1.
- In this way, the numbers are displayed.
#2
- Here, we have created a variable 'i' with value = 50.
- Then a condition is set [$i > 0] to display the numbers in descending order.
- Then we are decrementing the value of 'i' by writing incr $i -1. The number after variable tells how much the variable should be incremented.
- Hence, the problem is solved.
See attachments for output.
Attachments:
Similar questions