ASSIGNMENT 5
1a. Algorithm to Find the Sum of Positive Integers Entered by the User
Variable declarations:
num, sum are integer types
Step 1: Initialize sum ← 0
Step 2: Prompt user to enter a number
Step 3: Read num
Step 4: WHILE (num > 0) DO
sum ← sum + num
Prompt user to enter another number
Read num
END WHILE
Step 5: Print sum
Step 6: Stop
1b. Flowchart
The flowchart is shown below:
Start
Start
Sum 0
Prompt for number
Read number
No
Num > 0?
Yes
Sum Sum+Num
Prompt for number
Read Number
Print Sum
Stop
1c. Explanation of each line of Algorithm
Variable declarations: Define the necessary variables to store user input (num) and the
cumulative total (sum).
Step 1: Initialize sum to zero to begin accumulation from zero.
Step 2: Ask the user to input a number.
Step 3: Take the user’s input and store it in num.
Step 4: Begin a loop that checks if the number is greater than 0. If true, add the number
to sum, ask the user to enter another number, and repeat.
Step 5: After exiting the loop, print the total sum.
Step 6: End the algorithm.