Write a java program that will satisfy the given requirements:
A. It will display the given menu:
A - Arithmetic Series
B - Geometric Series
C - Harmonic Mean
D - Largest Prime Number
E - Largest Prime Number
Q - Quit the Program
B. The program will ask the user his/her choice.
C. If the choice is A,B,C,D or E, It will prompt the user for the required inputs, then compute and display the required outputs. If the choice is Q, the program will terminate
D. Validate all your inputs by promptingg for a new value if input is invalid.
E. Display also a description of your program
F. Program should execute for as long as the user wants to continue
G. Use of arrays is not allowed
import java.util.Scanner;
public class Main
{
static void displayAP(int a, int d, int n) {
for (int i = 1; i <= n; i++) {
System.out.print(a + (i - 1) * d + " ");
}
}
static void displayGP(int a, int r, int n) {
for (int i = 1; i <= n; i++) {
System.out.print(a * Math.pow(r, (i-1)+" ");
}
}
public static double harmonicMean(double[] data)
{
double sum = 0.0;
for (int i = 0; i < data.length; i++) {
sum += 1.0 / data[i];
}
return data.length / sum;
}
public static void main(String[] args) {
System.out.printf("A - Arithmetic Series\nB - Geometric Series\nC - Harmonic Mean\nD - Largest Prime Number");
System.out.printf("\nE - Largest Prime Number\nQ - Quit the Program");
Scanner scan = new Scanner(System.in);
char c = scan.next().charAt(0);
switch(c){
case 'A':
displayAP(1, 5, 20);
break;
case 'B':
displayGP(1,5,20);
break;
case 'C':
double [] arr = {90,32,24,57,100,199};
harmonicMean()
System.out.println("\nHarmonic mean is: "+harmonicMean(arr));
case 'Q':
System.exit(0);
}
}
}
Comments
Leave a comment