TCL script to find sum of n natural numbers using looping statements
Last Updated :
26 Apr, 2021
In this article, we will discuss the overview of TCL script and will cover the TCL script to find the sum of n natural numbers using looping statements with the help of an example. Let's discuss it one by one.
Pre-requisite -
You can go through this article to understand a few basics through this link. https://www.geeksforgeeks.org/basics-of-ns2-and-otcltcl-script/.
Overview :
We will understand the syntax of the while loop and for loop in Tool Command Language with a simple example. In this example, we will first use the while loop to find the sum of the first n natural numbers, and then we will see how to use for loop to accomplish the same. We will also compare the syntax with a familiar language to understand it better.
While-loop implementation :
We will discuss the implementation steps as follows.
Step-1 :
The first step is to read a number from the user after prompting to do so. To read the number we use gets, and we use puts to give a prompt.
puts "Enter a number"
gets stdin b
Step-2 :
Our next step is to initialize the sum to 0 and the iteration variable, i to 0. After this, we can have the while loop implementation of the code.
set sum 0
set i 0
while {$i<=$b} {
set sum [expr $sum+$i]
incr i
}
Note -
The syntax of the while loop must be exactly as shown above. If you neglect the spaces or type the opening curly brace in a new line, the result will be an error.
Step-3 :
The implementation of the while loop specified above would like the following in C programming as follows.
sum=0;
i=0;
while(i<=b)
{
sum=sum+i;
i++;
}
Step-4 :
Finally, the whole code and output are as follows.
Code -
puts "Enter a number"
gets stdin b
set sum 0
set i 0
while {$i<=$b} {
set sum [expr $sum+$i]
incr i
}
puts "The sum of first $b natural numbers is $sum"
Output :
For-loop implementation :
We will discuss the implementation steps as follows.
Step-1 :
The first 2 lines of code are the same as in the while loop. So, let's look at the for loop implementation part.
set sum 0
for {set i 1} {$i<=$b} {incr i} {
set sum [expr $sum+$i]
}
Note -
The syntax of for loop must be exactly as shown above. If you neglect the spaces or type the opening curly brace in a new line, the result will be an error.
Step-2 :
The implementation of the for loop specified above would like the following in C programming as follows.
sum=0;
for(i=1;i<=n;i++)
{
sum=sum+i;
}
Step-3 :
Finally, let's view the entire code and its output as follows.
Code -
puts "Enter a number"
gets stdin b
set sum 0
for {set i 1} {$i<=$b} {incr i} {
set sum [expr $sum+$i]
}
puts "The sum of first $b natural numbers is $sum"
Output :
Similar Reads
TCL script to demonstrate procedures In this article, we will know how to use procedures in TCL. Procedures are just like functions we use in any other programming language such as C, Java, Python, etc. Just like functions, procedures take arguments and return some value. Let's go through a simple program that calls procedures to print
4 min read
TCL script to demonstrate recursive procedures In this article, we will go through a unique set of TCL procedures that call themselves. These procedures as you might have already guessed are called recursive procedures. These are similar to recursive functions in other languages. Hence, to understand them even better, we will be considering the
3 min read
Python Program to Print the Natural Numbers Summation Pattern Given a natural number n, the task is to write a Python program to first find the sum of first n natural numbers and then print each step as a pattern. Input: 5 Output: 1 = 11 + 2 = 31 + 2 + 3 = 61 + 2 + 3 + 4 = 101 + 2 + 3 + 4 + 5 = 15 Input: 10 Output: 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 =
2 min read
JavaScript Program to Find the Sum of Natural Numbers In this article, we will learn how to find the sum of Natural Numbers. Given a number N and the task is to find the Sum of the first N Natural Numbers. These are the following methods, to get the sum of natural numbers: Using for LoopUsing RecursionUsing Mathematical formulaExamples: Input : n = 3Ou
2 min read
Sum of Sums first n natural numbers Given a positive integer n. The task is to find the sum of the sum of first n natural number.Examples: Input: n = 3Output: 10Explanation: Sum of first natural number: 1Sum of first and second natural number: 1 + 2 = 3Sum of first, second and third natural number = 1 + 2 + 3 = 6Sum of sum of first th
6 min read