Computer Science, asked by shorna2588, 1 year ago

Using a switch statement write a menu driven program to generate and display the first n terms of Fibonacci series the first two Fibonacci numbers are 0 and 1 and its subsequent number is the sum of previous two

Answers

Answered by abyssgaming2095
5

Answer:

as you want a switch statement for fibonacci series

Explanation:

#include <bits/stdc++.h>  

using namespace std;  

 

// Function to print first n Fibonacci Numbers  

void printFibonacciNumbers(int n)  

{  

   int f1 = 0, f2 = 1, i;  

 

   if (n < 1)  

       return;  

 

   for (i = 1; i <= n; i++)  

   {  

       cout<<f2<<" ";  

       int next = f1 + f2;  

       f1 = f2;  

       f2 = next;  

   }  

}  

 

// Driver Code  

int main()  

{  

   printFibonacciNumbers(7);  

   return 0;  

}  

Answered by divyaprakash007
0

Answer:

import java.util.*;

import java.io.*;

public class Program{

public static void main(String[] args) throws IOException{

 int no,m,f=1,c=0,p;

 InputStreamReader read = new InputStreamReader(System.in);

 BufferedReader in = new BufferedReader(read);

 System.out.println("1. Fibonacci Series");

       System.out.println("2. Sum of digits");

       System.out.print("Enter your choice: ");

       int ch = Integer.parseInt(in.readLine());

       switch (ch) {

           case 1:

           int a = 0, b = 1;

           System.out.print(a + " " + b);

           

           for (int i = 3; i <= 10; i++) {

               int term = a + b;

               System.out.print(" " + term);

               a = b;

               b = term;

           }

           break;

           case 2:

           System.out.print("Enter number: ");

           int num = Integer.parseInt(in.readLine());

           int sum = 0;

           while (num != 0) {

               sum += num % 10;

               num /= 10;

           }

           System.out.println("Sum of Digits " + " = " + sum);

           break;

           default:

           System.out.println("Incorrect choice");

           break;

       }

}

}

Explanation:

1. Fibonacci Series

2. Sum of digits

Enter your choice: 1

0 1 1 2 3 5 8 13 21 34

Similar questions