write an algoruthm and draw a flowchart to calculate the sum of natural numbers lies between 1 and 20
Answers
Answered by
0
We can start off by initializing 2 integer variables, ‘a’ and ‘sum’.
Let a = 1
Let sum = 0
Now, start a ‘for’ loop, as we know the number of times this algorithm should repeat itself.
for(i = 1; i <= 20; i++):
sum += i;
There’s another method to do this, but here, we will need to initialize our variables are doubles instead of integers.
Let i = 20
Let sum = 0
sum += i⋅ (i+1)2 ;
return sum;
This might seem extra, but we can try to emulate Carl Gauss to fund the solution to this problem.
Let i = 20 (terminal value)
Let s = 1 (starting value)
Similar questions