Computer Science, asked by monikamishra1010, 7 months ago

Q.5- Write a menu driven program to :
Generate and display the first 20 terms of the Fibonacci series 0, 1, 1, 2, 3,5....
To accept a number and display whether it is a prime number or not

Answers

Answered by anindyaadhikari13
3

\star\:\:\:\bf\large\underline\blue{Question:-}

Write a menu driven program to do the following.

  1. To generate and display the first 20 terms of the Fibonacci Series 0 1 1 2 3 5......
  2. To accept a number and display whether it is prime or not.

\star\:\:\:\bf\large\underline\blue{Source\:Code:-}

Here is your source code.

import java.util.*;

class MenuDriven

{

public static void main()

{

Scanner sc = new Scanner(System.in);

System.out.println("1. Print Fibonacci Series upto 20 terms. ");

System.out.println("Check for prime number.") ;

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

int ch=sc.nextInt();

switch(ch)

{

case 1:

int a=1,b=0,c;

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

for(int i=2;i<=20;i++)

{

c=a+b;

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

a=b;

b=c;

}

break;

case 2:

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

int x=sc.nextInt();

int d=0;

for(int i=1;i<=x;i++)

{

if(x%i==0)

d++;

}

if(d==2)

System.out.println("Number is Prime. ");

else

System.out.println("Number is not Prime. ");

break;

default: System.out.println("Invalid Choice. ");

}

} // end of main()

} // end of class.

Answered by namratavagish
1

Answer:

ere is your source code.

import java.util.*;

class MenuDriven

{

public static void main()

{

Scanner sc = new Scanner(System.in);

System.out.println("1. Print Fibonacci Series upto 20 terms. ");

System.out.println("Check for prime number.") ;

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

int ch=sc.nextInt();

switch(ch)

{

case 1:

int a=1,b=0,c;

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

for(int i=2;i<=20;i++)

{

c=a+b;

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

a=b;

b=c;

}

break;

case 2:

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

int x=sc.nextInt();

int d=0;

for(int i=1;i<=x;i++)

{

if(x%i==0)

d++;

}

if(d==2)

System.out.println("Number is Prime. ");

else

System.out.println("Number is not Prime. ");

break;

default: System.out.println("Invalid Choice. ");

}

} // end of main()

} // end of class.

Explanation:

Similar questions