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 stop
a = 0
b = 1
i = 1
while i <= n
write a
next = a + b
a = b
b = next
i = i + 1Python
def fibonacci(n):
if n < 1:
return []
terms = []
a, b = 0, 1
i = 1
while i <= n:
terms.append(a)
a, b = b, a + b
i += 1
return termsC
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, i;
if (n < 1) return;
for (i = 1; i <= n; i++) {
int next = a + b;
printf("%d ", a);
a = b;
b = next;
}
}- n = 1 writes 0. n = 5 writes 0, 1, 1, 2, 3.
- Some courses start at 1, 1, 2. This chart starts at 0 so the first two terms are the seeds stored in a and b.