Computer Science
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)
Java
Java Conditional Stmts
8 Likes
Answer
import java.util.Scanner;
public class KboatMean
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 for Arithmetic mean");
System.out.println("Enter 2 for Geometric mean");
System.out.println("Enter 3 for Harmonic mean");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter b: ");
int b = in.nextInt();
double mean = 0.0;
switch (choice) {
case 1:
mean = (a + b) / 2.0;
System.out.println("Arithmetic mean = " + mean);
break;
case 2:
mean = Math.sqrt(a * b);
System.out.println("Geometric mean = " + mean);
break;
case 3:
mean = 2.0 * a * b / (a + b);
System.out.println("Harmonic mean = " + mean);
break;
default:
System.out.println("Incorrect Choice!!!");
}
}
}
Output
Answered By
2 Likes
Related Questions
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 NumberA 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.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.
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