Computer Science, asked by kaladhar280, 10 hours ago

12. Write a tcl program ro print numbers from 1 to 50 in ascending and descending order using for loop.​

Answers

Answered by anindyaadhikari13
6

\textsf{\large{\underline{Solution}:}}

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

}

\textsf{\large{\underline{Explanation}:}}

#1

  1. At first,  we have created a variable named 'i' in the for loop with value = 1.
  2. Then a condition is set [$i < 51] to display the numbers from 1 to 50.
  3. Then we will increment the value of 'i'. When no value is given, the default value is taken as 1.
  4. In this way, the numbers are displayed.

#2

  1. Here, we have created a variable 'i' with value = 50.
  2. Then a condition is set [$i > 0] to display the numbers in descending order.
  3. 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.
  4. Hence, the problem is solved.

See attachments for output.

Attachments:
Similar questions