Print All The Fibonacci Term Up To N






Source code :-

#include<stdio.h>
int main()
{
    int n,a,b,y;
    printf("Enter the upper limit : ");
    scanf("%d",&n);
    a=0;
    b=1;
    y=1;
    printf("\nThe fibonacci numbers are : ");
    if(n>0)
        printf("%d",a);
    while(y<n)
    {
        printf(", %d",y);    
        y=a+b;
        a=b;
        b=y;
    }
    printf(".\n");    
    return 1;
}

Input-Output :-

abhi@hp-15q-laptop:~$ gcc fibonacci.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the upper limit :  5
The fibonacci numbers are : 0, 1, 1, 2, 3.

Print Fibonacci Series Up To N Term Using Recursion


Source code :

#include<stdio.h>
void call_fibo(int x)
{
    static int i;
    if(i>=x)
        return;
    i=i+1;
    printf("%d ",fibo(i));
    call_fibo(x);
}
int fibo(int n)
{
    if(n==0||n==1)
        return n;
    else
        return(fibo(n-1)+fibo(n-2));
}
int main()
{
    int x;
    printf("how many fibonacci terms you want: ");
    scanf("%d",&x);
    printf("\nThe fibonacci terms are : ");
    call_fibo(x);
    printf("\n");
    return 1;
}

Input-Output :

abhi@hp-15q-laptop:~$ gcc fibonacci.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the upper limit :  5
The fibonacci numbers are : 0, 1, 1, 2, 3

Post a Comment

أحدث أقدم