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