-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_fibonacci.c
67 lines (60 loc) · 1.77 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*************************************************************
DOCUMENTATION:
NAME :V.KARTHIEKEYAN
DATE :01.06.2021
DESCRIPTION :2.TO PRINT THE FIBONOCCI SERIRES UPTO INPUT 'N'
OUTPUT :./a.out
ENTER THE RANGE FOR FIBONACCI SERIES: -13
0, -1, 1, -2, 3, -5, 8, -13,
DO YOU WISH TO CONTINUE(y/Y): y
ENTER THE RANGE FOR FIBONACCI SERIES: -21
0, -1, 1, -2, 3, -5, 8, -13, 21,
DO YOU WISH TO CONTINUE(y/Y): n
*************************************************************/
#include<stdio.h>
int main()
{
int num, term_1 ,term_2 ,temp, check=1;
char choice='y'; //initialise choice as 'y'
for(int i=1;i<=20;i++) //to set the limits
check=check*2;
while (choice=='y' || choice=='Y') //while the user gives y continue to execute
{
printf("ENTER THE RANGE FOR FIBONACCI SERIES: ");
scanf("%d",&num);
if(num>-check && num<check) //if the num is within limits execute
{
term_1=0; //first term is 0
if(num<0) //for negative series
{
term_2=-1; //second as -1 for negative series
while(term_1>=num && -term_1>=num) //if both the term_1 and -term_1 are greater than given -ve num
{
printf("%d, ",term_1);
temp=term_1;
term_1=term_2;
term_2=temp-term_1;
}
printf("\n");
}
else if(num>=0)
{
term_2=1; //second term as 1 for positive series
while(term_1<=num) //when the term_1 is less than or equal to input
{
printf("%d, ",term_1);
temp=term_1;
term_1=term_2;
term_2=temp+term_1;
}
printf("\n");
}
}
else //error statement for input out of range
{
printf("NUMBER OUT OF RANGE, PLEASE ENTER THE VALUE -2^20<'N'<2^20\n");
}
printf("DO YOU WISH TO CONTINUE(y/Y): "); //asks input from user to continue
scanf(" %c",&choice); //scan the input choice
}
}