-->
Showing posts with label 8. Show all posts
Showing posts with label 8. Show all posts

program to get Fibonacci series 1,1,2,3,5,8,.... 20th term.

using codeblocks
-----------------------------------------------------------------------------------------------------
//program to get Fibonacci series 1,1,2,3,5,8,.... 20th term.

#include <stdio.h>  //header file
void fibonacci();//function prototype
int main()                 //main function
{
    fibonacci();          // function calling
    return 0;             // returning 0 value
}

void fibonacci()      //function body line/definition
{
    int a=1,b=1,c,i;       // initializing value 1
    printf("%d\n%d\n",a,b);// display of those value
    for(i=1;i<=18;i++)     // running loop for 18 times
    {
        c=a+b;            // finding sum of two numbers
        printf("%d\n",c);//display of that sum
        a=b;             //assigning b to a and c to b.
        b=c;
    }
}


----------------------------------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------

//program to get Fibonacci series 1,1,2,3,5,8,.... 20th term.

#include <stdio.h>  //header file
#include<conio.h>//header file
void fibonacci();//function prototype
int main()                 //main function
{
    fibonacci();          // function calling
    getch();             //holds the output
}

void fibonacci()      //function body line/definition
{
    int a=1,b=1,c,i;       // initializing value 1
    printf("%d\n%d\n",a,b);// display of those value
    for(i=1;i<=18;i++)     // running loop for 18 times
    {
        c=a+b;            // finding sum of two numbers
        printf("%d\n",c);//display of that sum
        a=b;             //assigning b to a and c to b.
        b=c;
    }
}

program to print/display 2,4,6,8,10,12... to 15th term.

//program to print/display 2,4,6,8,10,12... to 15th term.

#include<stdio.h>
#include<conio.h>
void main()
{
int k=2;

while(k<=15)
   {
      printf("%d,",k);
      k=k+2;
   }
getch();
}

Or
you can use following method.

first term(a)=2
common difference(d)=2
number of terms(n)=15
here  we can see that given series is in arithmetic progression so nth term=a+(n-1)*d.It gives us nth term=30 i.e.
last term will be 30.
We put 30 in while loop

//program to print/display 2,4,6,8,10,12... to 15th term.
#include<stdio.h>
#include<conio.h>
void main()
{
int k=2;

while(k<=30)
   {
      printf("%d,",k);
      k=k+2;
   }
getch();
}