C Program to Display Fibonacci Numbers
The C Program to find and display the " Fibonacci Numbers Sequence".
Here, We share some of the C Program for the Fibonacci Numbers Series. But first, we understand...
" What are Fibonacci Numbers? " & " How to find Fibonacci Numbers? "
The Fibonacci Numbers
Definition :
“ Fibonacci numbers are a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. "
" The simplest is the series 1, 1, 2, 3, 5, 8,... etc. ” code-box
" The simplest is the series 1, 1, 2, 3, 5, 8,... etc. ” code-box
The Fibonacci Numbers is a sequence of numbers where a number is an addition of the last two numbers, starting with 0, and 1.
For example: " The First 10 Fibonacci Numbers "
Always, First two Fibonacci Number is 0 and 1.
3. 0 + 1 = 1 4. 1 + 1 = 2
5. 2 + 1 = 3 6. 3 + 2 = 5
7. 5 + 3 = 8 8. 8 + 5 = 13
9. 13 + 8 = 21 10. 21 + 13 = 34 code-box
5. 2 + 1 = 3 6. 3 + 2 = 5
7. 5 + 3 = 8 8. 8 + 5 = 13
9. 13 + 8 = 21 10. 21 + 13 = 34 code-box
Now, You understand how to find Fibonacci Number. Only additon of two perivouse fibonacci numbers and you will get next Fibonacci number.
Forumal of Fibonacci Series
- xn is term number "n"
- xn−1 is the previous term (n−1)
- xn−2 is the term before that (n−2)
code-box
I hope, Now you just understend what is fibonacci number and how to find it.
Now see, an C program to display n number of Fibonacci Numbers or Sequence.
C Program to Display n Number of Fibonacci Sequence
Here, We using Loop and Swapping Method to display Fibonacci Sequence.
The For Loop use for repiting the proceese of display fibonacci number.
The Swapping method use for sawp calculeted Fibonacci number for finding next fibonacci number.
#include <stdio.h> int main() { int loop, num, present_num = 1, previous_num = 0, next_num = 0; // Get value from user for Limitetion printf("Enter a total number of Fibonacci Number: "); scanf("%d", &num); // Result of Fibonacci printf("Fibonacci Series: "); for (loop = 1; loop <= num; ++loop) { // Display the Fibonacci Number printf("%d, ", next_num); // Calculation of Fibonacci by Swaping next_num = present_num + previous_num; present_num = previous_num; previous_num = next_num; } return 0; }
C Program to Display Odd or Even numbers of Fibonacci Sequence
For displaying Odd and Even Fibonacci number, We just put one if() - if condition.
We just set an condition that if next_number module by 2 equal to zero [ next_number%2 == 0 ] then excute the display code.
This condiaiton will desipaly only even fibonacci numbers. And for Odd fibonacci number you just simpliy change one character of conditon; here it is...
next_number module by 2 not equal to zero [ next_number%2 != 0 ]
here we use if...else condition and we set else statment as continue for arrange sequens of fibonacci numbers.
Below, We written C Programe to performing following task
The C Programe to Dispaly Odd Fibonacci Numbers
#include <stdio.h>
int main()
{
int loop=0, num, present_num = 1, previous_num = 0, next_num = 0;
// Get value from user for Limitetion
printf("Enter a total number of Fibonacci Number: ");
scanf("%d", &num);
// Result of Fibonacci
printf("Fibonacci Series: ");
while (loop != num) {
// Calculation of Fibonacci by Swaping
next_num = present_num + previous_num;
present_num = previous_num;
previous_num = next_num;
// Display Odd the Fibonacci Number
if( next_num%2 != 0)
{
printf("%d, ", next_num);
}
else
{
continue;
}
// loop increment
loop++;
}
return 0;
}
The C Programe to Dispaly Even Fibonacci Numbers
#include <stdio.h>
int main()
{
int loop=0, num, present_num = 1, previous_num = 0, next_num = 0;
// Get value from user for Limitetion
printf("Enter a total number of Fibonacci Number: ");
scanf("%d", &num);
// Result of Fibonacci
printf("Fibonacci Series: ");
while (loop != num) {
// Calculation of Fibonacci by Swaping
next_num = present_num + previous_num;
present_num = previous_num;
previous_num = next_num;
// Display Even the Fibonacci Number
if( next_num%2 == 0)
{
printf("%d, ", next_num);
}
else
{
continue;
}
// loop increment
loop++;
}
return 0;
}
C Program to Find Fibonacci Numbers from Given Range
Below, We written C Programe to finding fibonacci numbers from given range.
Here, We chnage value of variables for while loop. We take to value from user which are the range. [ The value must be only integre ]
First value is starting value and second is end value. We set while loop to run as given range.
Here we use simple if condition. The condition was if next_num is greater than equal to start and less than equal to end then execute inner program code.
[ next_num>=start && next_num<=end ]
The C Programe to Find Fibonacci Numbers From Given Range
#include <stdio.h>
int main()
{
int loop=0, start, end, present_num = 1, previous_num = 0, next_num = 0;
// Get value from user for Limitetion
printf("Enter Renge [Start] : ");
scanf("%d", &start);
printf("Enter Renge [End] : ");
scanf("%d", &end);
// Result of Fibonacci
printf("Fibonacci Series: ");
while (start != end) {
// Calculation of Fibonacci by Swaping
next_num = present_num + previous_num;
present_num = previous_num;
previous_num = next_num;
// Display the Fibonacci Number
if( next_num>=start && next_num<=end )
{
printf("%d, ", next_num);
}
// loop increment
start++;
}
return 0;
}
You can apply many condtion with Fibonacci Series. By changeing same codes.
Post a Comment