जब रिकर्सन तकनीक का उपयोग किए बिना फिबोनाची सीरीज़ खोजने की आवश्यकता होती है, तो यूज़र से इनपुट लिया जाता है, और सीक्वेंस में नंबर प्राप्त करने के लिए 'while' लूप का उपयोग किया जाता है।
उदाहरण
इसके लिए एक प्रदर्शन नीचे दिया गया है:
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 से डिक्रीमेंट की जाती है।