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
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
Related Questions
A company deals with two types of customers (i.e. dealer and retailer) for selling its goods. The company also offers discount to the dealer and retailer at the time of purchasing goods for paying the bill, as per the tariff given below:
Days of payment Discount for Dealer Discount for Retailer Within 30 days 15% 10% 31 to 45 days 12% 8% 46 to 60 days 10% 5% More than 60 days No discount No discount Write a program in Java to accept:
(a) The number of days within which the bill is to be paid.
(b) The type of customer 'D' for dealer and 'R' for retailer.
(c) The amount of purchase.The program displays the details to the customer at the time of paying the bill.
A triangular number is formed by the addition of consecutive integers starting with 1. For example,
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
Thus, 3, 6, 10, 15, are triangular numbers.
Write a program in Java to display all the triangular numbers from 3 to n, taking the value of n as an input.Write a menu driven program using switch case statement to find the Arithmetic mean, Geometric mean and Harmonic mean which are calculated as:
(a) Arithmetic mean = (a + b) / 2
(b) Geometric mean = √ab
(c) Harmonic mean = 2ab / (a + b)
Write a program in Java to enter a natural number, where N>100 and N<1000, the natural number must not contain zeros. Print all the combinations of digits of the number including the number itself. Each new combination should appear on a new line. The program displays a message "Invalid Number", if N <100 or contains zeros.
Sample Input: Enter a number: 465
Sample Output:
456
465
546
564
645
654Sample Input: Enter a number: -712
Sample Output: Invalid NumberSample Input: Enter a number: 960
Sample Output: Invalid Number