Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS101 / KCS201 Programming for Problem Solving - Using C Language


Lab Exercises


15. Write a program to print the Fibonacci series.


/*
 File: Prgrm15.c
 Author: Aditya Saini
 Date: Sept 30, 2019
 Description: Program to print the Fibonacci series.
*/

#include <stdio.h>

int main (void)
{
               int terms;
               int previous, current, next;
               int i;

               //Input number of terms.
               printf ("Input number of terms: ");
               scanf ("%d", &terms);

               //Printing the Fibonacci Series.
               previous = 0;
               current = 1;
               printf ("%d terms of Fibonacci Series: %d  %d  ", terms, previous, current);

               //Finding the next term to print.
               for (i = 3; i <= terms; i++)
               {
                              next = previous + current;
                              printf ("%d  ", next);
                              previous = current;
                              current = next;
               }

               return 0;
};

Output

Input number of terms: 10
10 terms of Fibonacci Series: 0  1  1  2  3  5  8  13  21  34



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib