เมื่อจำเป็นต้องค้นหาอนุกรมฟีโบนักชีโดยไม่ต้องใช้เทคนิคการเรียกซ้ำ ระบบจะดึงข้อมูลอินพุตจากผู้ใช้และใช้ลูป "ในขณะที่" เพื่อให้ได้ตัวเลขในลำดับ
ตัวอย่าง
ด้านล่างนี้เป็นการสาธิตสำหรับสิ่งเดียวกัน -
first_num = int(input("Enter the first number of the fibonacci series... "))
second_num = int(input("Enter the second number of the fibonacci series... "))
num_of_terms = int(input("Enter the number of terms... "))
print(first_num,second_num)
print("The numbers in fibonacci series are : ")
while(num_of_terms-2):
third_num = first_num + second_num
first_num=second_num
second_num=third_num
print(third_num)
num_of_terms=num_of_terms-1 ผลลัพธ์
Enter the first number of the fibonacci series... 2 Enter the second number of the fibonacci series... 8 Enter the number of terms... 8 2 8 The numbers in fibonacci series are : 10 18 28 46 74 120
คำอธิบาย
- การป้อนหมายเลขแรกและตัวเลขที่สองถูกนำมาจากผู้ใช้
- จำนวนเงื่อนไขจะถูกนำมาจากผู้ใช้ด้วย
- ตัวเลขตัวแรกและตัวที่สองจะพิมพ์อยู่บนคอนโซล
- การวนรอบในขณะที่เริ่มต้น และด้านล่างเกิดขึ้น −
- ตัวเลขที่หนึ่งและที่สองจะถูกเพิ่มและกำหนดให้กับหมายเลขที่สาม
- หมายเลขที่สองถูกกำหนดให้กับหมายเลขที่สาม
- หมายเลขที่สามถูกกำหนดให้กับหมายเลขที่สอง
- หมายเลขที่สามพิมพ์อยู่บนคอนโซล
- จำนวนเทอมลดลง 1