Fibonacci Sequence Small program
Problem Analysis:Fibonacci sequence is characterized by the first two numbers are 1, from the third item, the first two and the number of the third item is summed up as:f1=f2=1 , F1=f1+f2 , F2=f1+f2 .
Program Source code:
#include <stdio.h>
#include <stdlib.h>
Main ()
{
int m,n,i,j=1;
Long F1,f2;
printf ("\t\t\t Fibonacci sequence Test applet \ n");/*** can omit ***/
printf (" Please enter the number of test data groups \ n");/*** can omit ***/
scanf ("%d", &n);
while (n--)
{
if (n+1)
{
printf ("\ n %d test \ n", j);
j + +;
}
printf (" Please enter the number of outputs required : \ n");/*** can omit ***/
scanf ("%d", &m);
F1=f2=1;
for (i=2;i<m;)
{
F1=F1+F2;
i++;
if (i==m) break;
F2=F1+F2;
i++;
if (i==m) break;
}
if (i%2==1)
printf (" number%d is %d\n", m,f1);
Else
printf (" number%d is %d\n", m,f2);
System ("pause");
System ("CLS");
printf ("\t\t\t Fibonacci sequence Test applet \ n");/*** can omit ***/
}
}
Program Analysis: While loop represents the number of test data groups, the main part of the program is the formula:f1=f2=1,f1=f1+f2, F2=f1+f2 the application. Since the number specified is not deterministic, it is necessary to determine the ordinal number of each item. Also need to know that the given sequence number is odd is even, then the odd output F1, is even output f2. Of course, F1,f2 can be assigned to the same variable, and then output this variable, there is no need to judge.
Fibonacci Sequence Small Program