-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_Fibonacci.c
44 lines (41 loc) · 1.49 KB
/
2_Fibonacci.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Name : Prabhat Kiran
Date : 02nd July 2022
Description : WAP to generate positive Fibonacci numbers.
Sample Input : 1) Enter a number: 8
2) Enter a number: 10
3) Enter a number: 21
4) Enter a number: -21
Sample Output: 1) 0, 1, 1, 2, 3, 5, 8
2) 0, 1, 1, 2, 3, 5, 8
3) 0, 1, 1, 2, 3, 5, 8, 13, 21
4) Invalid input
*/
#include <stdio.h>
int main()
{
int limit;
printf ("Enter a number: ");
scanf ("%d", &limit);
if (limit >= 0) //To check if the number entered by the user is positive or not.
{
int first = 0, second = 1, third = 0;
while (third <= limit) //The Fibonacci series will continue till the generated terms are less than or equal to the number entered by the user.
{
printf ("%d", third); //Print the term of the Fibonacci series.
first = second; //Swapping of the terms is done.
second = third;
third = first + second; //To generate the next term of the Fibonacci series.
if (third <= limit) //If the next term of the Fibonacci series generated is not the last term of the required series, then only the separator ',' needs to be printed.
{
printf (", ");
}
}
printf ("\n");
}
else //If the number is negative, print the error message.
{
printf ("Invalid input\n");
}
return 0;
}