**

<> Fibonacci sequence summation ——C language （ Small white version ）

**

<> Title Requirements

Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, 34…… It's not hard to find when n>2 Time ,an=an-1+an-2

requirement ： When screen input n（n>2） Time , Before output n Item and previous n Sum of items .

be careful ! We don't use recursion , No arrays, etc , We are Xiaobai who has studied the first three chapters ! So how C Language to solve ?

<> programming software

Visual Studio 2015
Compile run ：Ctrl + F5
Quick notes ：Ctrl + K Press again ：Ctrl + C
note off ：Ctrl + K Press again ：Ctrl + U

<> Start programming
// input n, Before input n term , And before n Item and // #include <stdio.h> int main() { int n,i,t; int sum = 0,
flag= 0; //flag Mark for newline int a1 = 1, a2 = 1; // The first two items are listed first printf(" Please enter the required number of items ："); scanf(
"%d", &n); printf("%d %d ", a1, a2); for (i = 3; i <= n; i++) // Start with Item 3 { t = a1
+ a2; printf("%d ", t); sum = sum + t; a1 = a2; // Pay attention to the way it is written here , assignment , Generate new a1,a2 a2 = t;
//flag++; //if (flag % 5 == 0) // Originally wanted to every 5 One output line , But I output it first a1,a2 // printf("\n");
// Causes the first line to have 7 individual , Not every 5 Tasks for row output // Put it here for everyone to solve , I think it will be a little troublesome } printf("\n\n front %d Item and is :%d\n", n, sum+
2); // Pay attention here sum The initial value needs to be added 2 return 0; }
<> Operation results
Please enter the required number of items ：7 1 1 2 3 5 8 13 front 7 Item and is ：33
<> Write at the end

1, Fight by hand , Try the input compiler , Try a different one n , Observe different results ;
2, Try to upgrade the algorithm , Change part of the code on the original basis , Realize richer functions ;
3, Welcome to leave a message , Later learned new skills , Try using an array , Function and other methods .

Technology
Daily Recommendation
views 2