Fibonacci Sequence Small Program

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.