Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
7. Write a program to construct a Fibonacci series up to n terms.
/*
File: Prgrm07.c
Author: Aditya Saini
Date: Jan 16, 2021
Description: Program to construct a Fibonacci series up to n terms.
*/
#include <stdio.h>
int main (void)
{
int n;
int previous, current, next;
int i;
//Input number of terms
printf ("Input number of terms: ");
scanf ("%d", &n);
//Printing the Fibonacci Series.
previous = 0;
current = 1;
printf ("%d terms of Fibonacci Series: %d %d ", n, previous, current);
//Finding the next term to print
for (i = 3; i <= n; i++)
{
next = previous + current;
printf ("%d ", next);
previous = current;
current = next;
}
};
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.