Programming Exercise 3 Nonrecursive Fibonacci 10 points The
Solution
solution
#include<stdio.h>
int main()
{
int choice;
int no;
int first=0;int second=1;int next,i;
printf(\"enter your choice\ \");
printf(\"1.fibonoci\ \");
printf(\"2.exit\ \");
scanf(\"%d\",&choice);
while(1)
{
printf(\"enter your choice\ \");
printf(\"1.fibonoci\ \");
printf(\"2.exit\ \");
scanf(\"%d\",&choice);
switch(choice)
{
case 1: printf(\"enter the no for finding fibonocci series \ \");
scanf(\"%d\",&no);
while(no<0)
{
printf(\"it should be a positive no (no>=1) retry\");
printf(\"enter a no\");
scanf(\"%d\",&no);
}
for(i=0;i<no;i++)
{
if(i<=1)
{
next=i;
}
else
{
next = first + second;
first = second;
second = next;
}
printf(\"%d\ \",next);
}
printf(\"the nth fibonocci no is %d\ \",next);
break;
case 2:printf(\"Thank you\ \");
exit(0);
default:printf(\"please choose correct option\ \");
break;
}
}
return 0;
}
output
enter your choice
1.fibonoci
2.exit
1
enter the no for finding fibonocci series
10
0
1
1
2
3
5
8
13
21
34
the nth fibonocci no is 34
enter your choice
1.fibonoci
2.exit
1
enter the no for finding fibonocci series
-5
it should be a positive no (no>=1) retry
enter a no2
0
1
the nth fibonocci no is 1
enter your choice
1.fibonoci
2.exit
2
Thank you
--------------------------------
Process exited after 19.03 seconds with return value 0
Press any key to continue . . .

