1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sp2606 [1]
3 years ago
13

A python script to print fibonacci series first 20 elements

Computers and Technology
1 answer:
madreJ [45]3 years ago
5 0

Answer:

   nterms =20

   # first two terms

   n1, n2 = 0, 1

   count = 0

   # check if the number of terms is valid

   if nterms <= 0:

      print("Please enter a positive integer")

   elif nterms == 1:

      print("Fibonacci sequence upto",nterms,":")

      print(n1)

   else:

      print("Fibonacci sequence:")

      while count < nterms:

          print(n1)

          nth = n1 + n2

          # update values

          n1 = n2

          n2 = nth

          count += 1

Explanation:

Here, we store the number of terms in nterms. We initialize the first term to 0 and the second term to 1.

If the number of terms is more than 2, we use a while loop to find the next term in the sequence by adding the preceding two terms. We then interchange the variables (update it) and continue on with the process.

<em>output:</em>

Fibonacci sequence:

0

1

1

2

3

5

8

You might be interested in
Stay safe and search answers up if you need ​
Nostrana [21]

Answer:

Yes, Thank you

Jesus loves you!!

Explanation:

3 0
3 years ago
1) The program reads an integer, that must be changed to read a floating point. 2) You will need to move that number into a floa
DochEvi [55]

Answer:

1. Get the number

2. Declare a variable to store the sum and set it to 0

3. Repeat the next two steps till the number is not 0

4. Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum.

5. Divide the number by 10 with help of ‘/’ operator

6. Print or return the sum

# include<iostream>

using namespace std;

/* Function to get sum of digits */

class gfg

{

   public:

   int getSum(float n)

   {

   float sum = 0 ;

   while (n != 0)

   {

    sum = sum + n % 10;

    n = n/10;

   }

return sum;

}

};

//driver code

int main()

{

gfg g;

float n = 687;  

cout<< g.getSum(n);

return 0;

}

Explanation:

6 0
3 years ago
Which setting indents all but the first line of a paragraph by the selected length?
Phoenix [80]

Answer:

Hanging

Explanation:

5 0
3 years ago
PLEASE HELP WITH MY COMPUTER
qaws [65]

Answer:

try powering it off the turn it back on

Explanation:

5 0
3 years ago
Read 2 more answers
How do I give Brainliest? Will give brainliest when I figure out how
Lina20 [59]
When somebody writes the answer for your question and posts it on your question it will show a “(crown)mark as brainliest” in blue. click it and it’ll turn yellow and mark the person the brainliest for your question
7 0
3 years ago
Read 2 more answers
Other questions:
  • A(n) ____ loop executes a predetermined number of times.
    12·1 answer
  • How to go to a website with an ip address?
    15·1 answer
  • Ebba received a message from one of her tech support employees. In violation of company policy, a user had downloaded a free pro
    14·1 answer
  • You view a portion of a document on the screen through a
    14·1 answer
  • Write a Unix (Linux) find-like command (myFind) in Python3 where the function will return the full paths of all the files contai
    9·1 answer
  • Assume that an array named salarySteps whose elements are of type int and that has exactly five elements has already been declar
    12·1 answer
  • cpp g Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print fo
    15·1 answer
  • Project introduction​
    14·1 answer
  • 17. What are the basic modes of operation of 8255?Write the features of mode 0 in 8255?
    8·1 answer
  • Sharl downloads images from an online library and uses them in her work. The images are shared under the Creative Commons Attrib
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!