Answer to Question #259366 in Java | JSP | JSF for deek

Question #259366

Write a java program that will satisfy the given requirements using while loop:




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


1
Expert's answer
2021-10-31T18:41:53-0400


package menubasedprogram;


import java.util.Scanner;




public class MenuBasedProgram {


     static boolean is_prime(int number)
    {
        
        if (number <= 1)
            return false;
  
        
        for (int i = 2; i < number; i++)
            if (number % i == 0)
                return false;
  
        return true;
    }
    
   static void displayArithmetic(int first, int d, int n)
{
 


int term;
term=first;
for (int i = 1; i <= n; i++)
{   
    System.out.printf("%d  ", term);
    term =term + d;
     
}
}
   
     static void printGeometric (int first, int r, int n)
{
 


int term;
term=first;
for (int i = 1; i <= n; i++)
{   
    System.out.printf("%d  ", term);
    term =term * (int) Math.pow(r, i);
     
}
}
    public static void main(String[] args) {
     
        System.out.printf("\nA - Arithmetic Series\nB - Geometric Series\nC - Harmonic Mean");
  System.out.printf("\nD - Largest Prime Number\nE - Largest Prime Number\nQ - Quit the Program\n");
  
  Scanner scan  = new Scanner(System.in);
  char choice= scan.next().charAt(0);
  switch(choice){
      case 'A':
          System.out.println("Enter the first term");
          int first = scan.nextInt();
          System.out.println("Enter the number of terms");
          int n = scan.nextInt();
          System.out.println("Enter the common term");
          int d = scan.nextInt();
          displayArithmetic(first, d, n);
          break;
      case 'B':
          System.out.println("Enter the first term");
          int f = scan.nextInt();
          System.out.println("Enter the number of terms");
          int number = scan.nextInt();
          System.out.println("Enter the common term");
          int r = scan.nextInt();
          printGeometric(f, r, number);
          break;
      case 'C':
          System.out.println("Enter the number of terms");
          int n1 = scan.nextInt();
          double sum=0;	
	for(int i=1;i<=n1;i++)
	{
		sum=sum+(1/i);
	}
	System.out.println("Harmonic value = "+sum);
          
          break;
          
      case 'D':
           System.out.println("Enter the number of terms");
          int n2 = scan.nextInt();
          for(int i=n2; i>=1; i--){
              if(is_prime(i)){
                  System.out.println(i);
                  break;
              }
          }
      break;
      case 'E':
           System.out.println("Enter the number of terms");
          int n3 = scan.nextInt();
          for(int i=n3; i>=1; i--){
              if(is_prime(i)){
                  System.out.println(i);
                  break;
              }
          }
      break;
          
      case 'Q':
          System.exit(0);
          
  }
  
        
        
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS