Programming Exercise 3 Nonrecursive Fibonacci 10 points The

Programming Exercise 3: Non-recursive Fibonacci (10 points The Fibonacci sequence is l, l, 2, 3, 5, 8, 13, 21, 34, 55, he first term is 1. The second term is 1. Each term thereafter is the sum of the previous two terms) Write a C program that contains a function called fibonacci. This function, when given an integer tha strategy. The main function should provide the user with a menu of two choices: 1- Calculate Fibonacci 2- Exit (Enter 1 or 2) If the user enters a choice different from 1 or 2, your program should detect the invalid choice, and re-prompt the user to enter a new valid choice. If the user picks choice 1, your program should prompt him her to enter an integer number greater than 0. This number is first validated by the program, then function Fibonacci is called, and the result is displayed to the user The menu is continually displayed until choice 2 is entered by the user. Use a switch statement for controlling the menu. Here is an example of execution:

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 . . .

 Programming Exercise 3: Non-recursive Fibonacci (10 points The Fibonacci sequence is l, l, 2, 3, 5, 8, 13, 21, 34, 55, he first term is 1. The second term is 1
 Programming Exercise 3: Non-recursive Fibonacci (10 points The Fibonacci sequence is l, l, 2, 3, 5, 8, 13, 21, 34, 55, he first term is 1. The second term is 1

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site