Friday, March 18, 2016

C Program to print the fibonacci series upto 'n' terms except first two

/*=================================================================================
 **
 **  File Name     : fibonacci.c
 **  Creation Date : Sat 19 Mar 2016 03:58:49 AM IST
 **  Last Modified : Sat 19 Mar 2016 04:14:17 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = Fibonacci Series upto 'n' terms = = = = =\n");
    int t1, t2, n, sum, count = 0;
    printf("\nEnter the first term : ");
    scanf("%d",&t1);
    printf("Enter the second term : ");
    scanf("%d",&t2);
    printf("Enter the number of terms you want to print : ");
    scanf("%d",&n);
    printf("\nThe first two term of fibonacci series are : %d , %d\n", t1, t2);
    printf("Remaining %d terms are : ",n);
    while (count < n)
    {
        sum = t1 + t2;
        printf("%d  ", sum);
        t1 = t2;
        t2 = sum;
        count++;
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = =\n");
}

OutPut:

max@ubuntu:~/cprog$ gcc fibonacci.c -o fibonacci
max@ubuntu:~/cprog$ ./fibonacci

= = = = = Fibonacci Series upto 'n' terms = = = = =

Enter the first term : 3
Enter the second term : 5
Enter the number of terms you want to print : 10

The first two term of fibonacci series are : 3 , 5
Remaining 10 terms are : 8  13  21  34  55  89  144  233  377  610 

= = = = = = = = = = = = = = = = = = = = = = = = = =

No comments:

Post a Comment