Generation of Fibonacci Series -C Program

Program:
#include<stdio.h>
//#include<conio.h>
void main()
{
    int a[200];int i,n,t1=1,t2=1,t3,x,y;//clrscr();
    a[1]=1;a[2]=1;
    printf("Enter the no of terms:");
    scanf("%d",&n);
    if(n<=0)
        printf("Error..!");
    else if(n==1)
        printf("The fibonacci series:\n1");
    else if(n==2)
        printf("The fibonacci series:\n1,1");
    else
        printf("The fibonacci series:\n1,1");
        for(i=3;i<=n;i++)
            {
                t3=t1+t2;
                t1=t2;
                t2=t3;
                printf(",%d",t2);
                a[i]=t3;
            }
    printf(",...\n");
    //getch();
}

Output:
nn@linuxmint ~ $ gcc c4.c
nn@linuxmint ~ $ ./a.out
Enter the no of terms:5
The fibonacci series:
1,1,2,3,5,...
nn@linuxmint ~ $

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...