Fibonacci Series in C Using Function

Fibonacci Series in C Using Function | Previously we have written the Fibonacci series program in C. Now, we will develop the same but using function. In this post, we will write the Fibonacci series in C using the function.

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming. The function is a small program that is used to do a particular task. In C, a big program is divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

The C program is made of one or more pre-defined/user-defined functions. Every program must have at least one function with the name main. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.

In the Fibonacci series, the next element will be the sum of the previous two elements. The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth.

Written, as a rule, the expression is:- Xn= Xn-1+ Xn-2

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

Doing your C assignment can often be challenging and time-consuming.

If you’re stuck on a project or just need help understanding a topic, assignment service can provide reliable assistance. Experienced tutors specialize in C topics such as data structures, Fibonacci sequences, and more. With help, you’ll have the confidence to tackle any assignment, no matter how tough. You can get help in do my C homework so that you can go on to use this powerful language for all of your programming needs.

Display Fibonacci series in C within a range using a function

#include<stdio.h>
void fibonacciSeries(int range)
{
   int a=0, b=1, c;
   while (a<=range)
   {
     printf("%d\t", a);
     c = a+b;
     a = b;
     b = c;
   }
}

int main()
{
   int range;

   printf("Enter range: ");
   scanf("%d", &range);

   printf("The fibonacci series is: \n");

   fibonacciSeries(range);

   return 0;
}

Output for different test-cases:-

Enter range: 20
The fibonacci series is:
0 1 1 2 3 5 8 13

Enter range: 50
The fibonacci series is:
0 1 1 2 3 5 8 13 21 34

Display fibonacci series upto Nth term

#include<stdio.h>
void fibonacciSeries(int n)
{
   int a=0, b=1, c;
   for(int i=0; i<n; i++)
   {
     printf("%d\t", a);
     c = a+b;
     a = b;
     b = c;
   }
}

int main()
{
   int term;

   printf("Enter the term: ");
   scanf("%d", &term);

   printf("The fibonacci series is: \n");

   fibonacciSeries(term);

   return 0;
}

Output for different test-cases:-

Enter the term: 5
The fibonacci series is:
0 1 1 2 3

Enter the term: 10
The fibonacci series is:
0 1 1 2 3 5 8 13 21 34

Find nth term of Fibonacci series in C using function

#include<stdio.h>
int fibonacciTerm(int n)
{
   int a=0, b=1, c;
   for(int i=1; i<n; i++)
   {
     c = a+b;
     a = b;
     b = c;
   }
   return a;
}

int main()
{
   int term, result;

   printf("Enter term to find: ");
   scanf("%d", &term);

   result = fibonacciTerm(term);

   printf("The fibonacci term is: %d", result);

   return 0;
}

Output for test-case-1:-

Enter the term to find: 5
The Fibonacci term is: 3

Output for test-case-2:-

Enter the term to find: 10
The Fibonacci term is: 34

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Follow Us
Instagram • Twitter • Youtube • Linkedin • Facebook • Pinterest • Telegram

Leave a Comment

Your email address will not be published. Required fields are marked *