Fibonacci Series up to 100 in C Program
Fibonacci series up to 100 in c program.
In this article, you will learn how to generate the Fibonacci series up to Nth positive numbers. We'll use various approaches to solve this problem like for loop
, while loop
, do-while loop
, etc.
Example
INPUT POSITIVE NUMBER: 100
FIBONACCI SERIES: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
You should know about the following topics in C programming to understand this program:
- For loop in C
- While loop in C
- Do-while loop in C
- Function in C
- Recursion in C
1 - Fibonacci Series up to 100 in C using For loop
// Fibonacci Series up to 100 in C using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num;
int term_1 = 0, term_2 = 1, next_term = 0;
// Step-1 Take input from the User
printf("INPUT POSITIVE NUMBER: ");
scanf("%d", &num);
// Step-2 Displays the first two terms which are always 0 and 1
printf("\nFIBONACCI SERIES: %d, %d, ", term_1, term_2);
// Step-3 Use a loop in this range of 1 to N positive numbers and get the next terms
for (next_term = term_1 + term_2; next_term <= num; next_term = term_1 + term_2)
{
printf("%d, ", next_term);
term_1 = term_2;
term_2 = next_term;
}
return 0;
}
Output
INPUT POSITIVE NUMBER: 100
FIBONACCI SERIES: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
2 - Fibonacci Series up to 100 in C using While loop
// Fibonacci Series up to 100 in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num;
int term_1 = 0, term_2 = 1, next_term = 0;
// Step-1 Take input from the User
printf("INPUT POSITIVE NUMBER: ");
scanf("%d", &num);
// Step-2 Displays the first two terms which are always 0 and 1
printf("\nFIBONACCI SERIES: %d, %d, ", term_1, term_2);
// Step-3 Use a loop in this range of 1 to N positive numbers and get the next terms
next_term = term_1 + term_2;
while (next_term <= num)
{
printf("%d, ", next_term);
term_1 = term_2;
term_2 = next_term;
next_term = term_1 + term_2;
}
return 0;
}
3 - Fibonacci Series up to 100 in C using Do-while loop
// Fibonacci Series up to 100 in C using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num;
int term_1 = 0, term_2 = 1, next_term = 0;
// Step-1 Take input from the User
printf("INPUT POSITIVE NUMBER: ");
scanf("%d", &num);
// Step-2 Displays the first two terms which are always 0 and 1
printf("\nFIBONACCI SERIES: %d, %d, ", term_1, term_2);
// Step-3 Use a loop in this range of 1 to N positive numbers and get the next terms
next_term = term_1 + term_2;
do
{
printf("%d, ", next_term);
term_1 = term_2;
term_2 = next_term;
next_term = term_1 + term_2;
} while (next_term <= num);
return 0;
}
Output
INPUT POSITIVE NUMBER: 200
FIBONACCI SERIES: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
4 - Fibonacci Series up to 100 in C using Function
// Fibonacci Series up to 100 in C using Function
#include <stdio.h>
// Function to generate the Fibonacci series
int generate_fibonacci(int num)
{
int term_1 = 0, term_2 = 1, next_term = 0;
printf("%d, %d, ", term_1, term_2); // Prin the initial terms of fibonacci series
next_term = term_1 + term_2;
while (next_term <= num)
{
printf("%d, ", next_term);
term_1 = term_2;
term_2 = next_term;
next_term = term_1 + term_2;
}
}
// It's the main function of the program
int main()
{
int num;
// Step-1 Take input from the User
printf("INPUT POSITIVE NUMBER: ");
scanf("%d", &num);
// Step-2 Call the generate_fibonacci() function to generate the fibonacci series
printf("\nFIBONACCI SERIES: ");
generate_fibonacci(num);
return 0;
}
Output
INPUT POSITIVE NUMBER: 60
FIBONACCI SERIES: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
5 - Fibonacci Series up to 100 in C using Recursion
// Fibonacci Series up to 100 in C using Recursion
#include <stdio.h>
// Recursive Function to generate the Fibonacci series
int rec_fibonacci(int num, int term_1, int term_2, int next_term)
{
printf("%d, ", next_term);
term_1 = term_2;
term_2 = next_term;
next_term = term_1 + term_2;
if (next_term <= num)
rec_fibonacci(num, term_1, term_2, next_term);
else
return 0;
}
// It's the main function of the program
int main()
{
int num;
int term_1 = 0, term_2 = 1, next_term = 0;
// Step-1 Take input from the User
printf("INPUT POSITIVE NUMBER: ");
scanf("%d", &num);
// Step-2 Print the first two terms which are always 0 and 1
printf("\nFIBONACCI SERIES: %d, %d, ", term_1, term_2);
next_term = term_1 + term_2;
// Step-3 Call the recursive function:rec_fibonacci() to generate the fibonacci series
rec_fibonacci(num, term_1, term_2, next_term);
return 0;
}
Output
INPUT POSITIVE NUMBER: 500
FIBONACCI SERIES: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,