c program to generate fibonacci series using Function

Spread the love

Description :

In our previous post, we discussed about what is Fibonacci Series and C program to Generate Fibonacci Series. Now in this post we are going to write C program to calculate first n Fibonacci numbers of Fibonacci Series but this using function.

This Program Accept one Input from user ( let’s say it as n ) and prints first n Fibonacci numbers of Fibonacci Series.

Program :

  1. * Fibonacci series Using Function */
  2. /* Author : Venkatesh macha */
  3. #include<stdio.h>
  4. void print_fibonacci(int num)
  5. {
  6.         int i,j,k,cnt;
  7.         i = 0;  /* Fibonacci series starts with 0 and 1 so.*/
  8.         j = 1;
  9.         printf(“%d %d “,i,j);
  10.         cnt = 2;
  11.         k = i + j; /* cnt is 2 because we already printed 0 and 1*/
  12.         while(cnt < num)
  13.         {
  14.                 printf(“%d “,k);
  15.                 i = j;
  16.                 j = k;
  17.                 k = i+j;
  18.                 cnt++;
  19.         }
  20.         printf(n);
  21.         return;
  22. }
  23. int main()
  24. {
  25.         int num;
  26. LABEL:  printf(“Enter how many fibonacci numbers to print : “);
  27.         scanf(“%d”,&num);
  28.         if(num < 3)
  29.         {
  30.                 printf(” please enter more than 2 n);
  31.                 goto LABEL;
  32.         }
  33.         print_fibonacci(num);
  34.         return 0;
  35. }

Output :

C Program to Calculate Fibonacci Number using Function

Note : This program accepts only Numbers above 2 (because Fibonacci Series starts with 0 and 1). If you enter 0 or 1 it will simply display one Error message and asks for one more value. 

In above program print_fibonacci is function. returns nothing and Accepts one Integer.
here is the prototype of print_fibonacci.

void print_fibonacci( int );

More programs on Fibonacci Series :

Related Programs C Programs :

  1. C Program to Calculate Grade of Student.
  2. C program to print Hello world.
  3. Welcome note from SillyCodes.
  4. C program to Covert Temparature
  5. C program to find area and perimeter of Rectangular.

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

Leave a Reply