KnowledgeBoat Logo

Computer Science

Write a menu driven program using switch case statement to perform the given tasks as stated.
Task 1:
A Fibonacci series is given as:
0, 1, 1, 2, 3, 5, 8, 13, ………. and so on.
Display all the prime Fibonacci numbers in the range from 1 to 1000.
For example, 2, 3, 5, 13 are prime fibonacci numbers.
Task 2:
Prime factors are the factors of a number which are prime numbers. Display all the prime factors of a number.
Sample Input: 24
Sample Output: The prime factors of 24 are: 2, 2, 2, 3

Java

Java Iterative Stmts

3 Likes

Answer

import java.util.Scanner;

public class KboatFibonacciPrimeFactors
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 1 Prime Fibonacci Numbers");
        System.out.println("Enter 2 Prime factors");
        System.out.print("Enter your choice: ");
        int choice = in.nextInt();

        switch (choice) {
            case 1:
            int a = 0, b = 1;
            int term = a + b;
            while (term <= 1000) {
                /*
                 * Check if the this term of
                 * Fibonacci Series is Prime
                 * or not
                 */
                int c = 0;
                for (int i = 1; i <= term; i++) {
                    if (term % i == 0) {
                        c++;
                    }
                }

                if (c == 2)
                    System.out.print(term + " ");
                    
                a = b;
                b = term;
                term = a + b;
            }
            break;

            case 2:
            System.out.print("Enter Number: ");
            int num = in.nextInt();
            System.out.print("The prime factors of " + num + " are: ");
            for(int i = 2; i < num; i++) {
                while (num % i == 0) {
                    System.out.print(i + " ");
                    num = num / i;
                }
            }
            if(num > 2) {
                System.out.println(num);
            }
            break;

            default:
            System.out.println("Incorrect Choice!!!");
        }
    }
}

Output

BlueJ output of Write a menu driven program using switch case statement to perform the given tasks as stated. Task 1: A Fibonacci series is given as: 0, 1, 1, 2, 3, 5, 8, 13, ………. and so on. Display all the prime Fibonacci numbers in the range from 1 to 1000. For example, 2, 3, 5, 13 are prime fibonacci numbers. Task 2: Prime factors are the factors of a number which are prime numbers. Display all the prime factors of a number. Sample Input: 24 Sample Output: The prime factors of 24 are: 2, 2, 2, 3BlueJ output of Write a menu driven program using switch case statement to perform the given tasks as stated. Task 1: A Fibonacci series is given as: 0, 1, 1, 2, 3, 5, 8, 13, ………. and so on. Display all the prime Fibonacci numbers in the range from 1 to 1000. For example, 2, 3, 5, 13 are prime fibonacci numbers. Task 2: Prime factors are the factors of a number which are prime numbers. Display all the prime factors of a number. Sample Input: 24 Sample Output: The prime factors of 24 are: 2, 2, 2, 3

Answered By

2 Likes


Related Questions