Here’s a C program to find the Fibonacci series with output and proper explanation. The program uses for loop.
What is Fibonacci Series? By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. e.g.0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
# include <stdio.h> # include <conio.h> void main() { int a = -1, b = 1, c = 0, i, n; clrscr() ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThe fibonacci series is : \n\n") ; for(i = 1 ; i <= n ; i++) { c = a + b ; printf("%d \t", c) ; a = b ; b = c ; } getch() ; }
Output of above program
Enter the limit : 5
The fibonacci series is : 0 1 1 2 3
Explanation of above program
This program calculates the Fibonacci series up to a limit and print the output back to the user.
The program has several variables -
- a, b, c - These integer variables are used for the calculation of Fibonacci series. The values of a, b and c are initialized to -1, 1 and 0 respectively.
- n - This integer is the limit determining the number of elements of Fibonacci series that should be calculated.
- i - This is the loop variable.
First the user enters a limit. After that for loop takes the control and
calculates the Fibonacci series up to the previously entered
limit.
To understand the working of for loop in this program, let’s take an
example. Suppose the limit is 5 i.e. n = 5. So the for loop will
executes for i = 1 to 5. Let’s go through each step one by one.
Note: The process mentioned below is the standard way to calculate Fibonacci series so just remember the steps as is.
Process for i = 1
- First we update the value of c (initially 0) with the following expression - c = a + b. So after this c = -1 + 1 = 0 (first value of Fibonacci series).
- Then we print the value of c. Notice \t is used to provide 8 spaces (1 tab) between two values see in the output.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 1 and b = 0.
Process for i = 2
- We again update the value of c like before. So after this c = a + b = 1 + 0 = 1 (second value of Fibonacci series).
- Then we print the value of c.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 0 and b = 1.
Process for i = 3
- We again update the value of c like before. So after this c = a + b = 0 + 1 = 1 (third value of Fibonacci series).
- Then we print the value of c.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 1 and b = 1.
Process for i = 4
- We again update the value of c like before. So after this c = a + b = 1 + 1 = 2 (fourth value of Fibonacci series).
- Then we print the value of c.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 1 and b = 2.
Process for i = 5
- We again update the value of c like before. So after this c = a + b = 1 + 2 = 3 (third value of Fibonacci series).
- Then we print the value of c.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 2 and b = 3.
This is how we calculate the Fibonacci series.
Also look at: Program to Calculate Sum of Fibonacci Series.
Tip: To understand the program you can also take different values of n and try the above process yourself. It will help you to understand the process much clearly and helps in improving your debugging ability.
write a function that return the minimum and maximum value in an array of innteger .input to the function are the array to the interger ,an integer varible containing the length of the array and pointer to the integer varible that will contain the maximum and minimum value.the function prototype is------
VOID MINMAX(INT ARRAY[], INTLENGTH,INT*MIN,INT*MAX)
Check this out: http://cprogramming.language-tutorial.com/2013/04/finding-max-and-min-of-array-elements.html
Hope it helps... :)
thnx a lot...
using file concept write a program to input a string and output the reversed string.you are not to use array notation to access the character ,insted use pointer notation...
plzz help me in this also....
thnx
Check this out: http://cprogramming.language-tutorial.com/2013/04/reverse-of-string-using-pointers-in-c.html
Hope this is what you were looking for... :)
thank you ....
Any good book for C beginners?
Bonjour,
Three cheers to you ! Hooray!!! I feel like I hit the jackpot on Program to Find Fibonacci Series in C | C Program
i have this code
int i=3;
int j=(++x)*(++x);
now j become 25, why?
cant explain it, why not 4*5=20?
Follow my new blog if you interested in just tag along me in any social media platforms!
Obrigado,
Irene Hynes