ลำดับฟีโบนัชชี (Fibonacci Series) คือลำดับตัวเลขที่ได้จากการบวกสองตัวเลขก่อนหน้า
ลำดับฟีโบนัชชีเริ่มจากสองตัวเลข f0 และ f1
ค่าเริ่มต้นของ f0 และ f1 สามารถเป็น 0, 1 หรือ 1, 1 ลำดับฟีโบนัชชีเป็นไปตามเงื่อนไข:
fn = fn-1 + fn-2
อัลกอริทึม
อัลกอริทึมสำหรับลำดับฟีโบนัชชี:
START Step 1: อ่านตัวแปรจำนวนเต็ม a, b, c ระหว่างรัน Step 2: กำหนด a = 0 และ b = 0 Step 3: คำนวณ c = a + b Step 4: พิมพ์ c Step 5: กำหนด a = b, b = c Step 6: ทำซ้ำ 3 ถึง 5 จำนวน n ครั้ง STOP
ตัวอย่าง
โปรแกรม C สำหรับลำดับฟีโบนัชชี ใช้ลูป While:
#include <stdio.h>
int main() {
int number, i = 0, next, first = 0, second = 1;
printf("\n Please Enter the Range Number: ");
scanf("%d", &number);
while(i < number) {
if(i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d \t", next);
i++;
}
return 0;
}ผลลัพธ์
เมื่อรันโปรแกรม จะได้ผลลัพธ์ดังนี้:
Please Enter the Range Number: 6 0 1 1 2 3 5