Question #318556

Solve the following problem by showing the design of your solution as an algorithm.


The problem – The program will take two integers from the user, say n1 and n2. It will then print the sum of all numbers between n1 and n2, inclusive. For example, if the user enters 5 and 9, the program will print the value of 5+6+7+8+9. On the other hand, if the user enters 5 and -2, the program will print the value of 5+4+3+2+1+0+(-1)+(-2). Note: Do not assume the user will always enter the smaller number first.

Expert's answer

1 Read n1

2 Read n2

3 If n1 < n2

4 Set step equal 1

5 Else

6 Set step equal -1

7 Set sum equal 0

8 Set i equal n1

9 If i equal n2 go to step 13

10 Add i to sum

11 Add step to i

12 Go to step 9

13 Print sum



LATEST TUTORIALS
APPROVED BY CLIENTS