What does the flowchart look like?
Ready to draw?
No signup. Opens instantly in your browser.
What is the matching code?
Pseudocode
read n
if n < 1 then sum = 0
else
sum = 0
i = 1
while i <= n
sum = sum + i
i = i + 1Python
def sum_to(n):
if n < 1:
return 0
total = 0
i = 1
while i <= n:
total += i
i += 1
return totalC
int sum_to(int n) {
int total = 0, i;
if (n < 1) return 0;
for (i = 1; i <= n; i++) total += i;
return total;
}- This adds 1 through n. It does not read n separate values from the user.
- For n = 3 the additions are 1, then 2, then 3, and the total is 6.