Answer to Question #257866 in Java | JSP | JSF for niek

Question #257866

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



1
Expert's answer
2021-10-29T00:54:41-0400


SOLUTION CODE FOR THE ABOVE QUESTION


package com.company;

import java.math.*;
import java.util.Scanner;


public class Main{

    static boolean checkForPrime(int inputNumber)
    {
        boolean isItPrime = true;

        if(inputNumber <= 1)
        {
            isItPrime = false;

            return isItPrime;
        }
        else
        {
            for (int i = 2; i<= inputNumber/2; i++)
            {
                if ((inputNumber % i) == 0)
                {
                    isItPrime = false;

                    break;
                }
            }

            return isItPrime;
        }
    }

    public static void main(String []args){

        Scanner sc = new Scanner(System.in);
        int exit_condition = 1;
        while (exit_condition == 1)
        {
        System.out.println("Enter the option for operation to perform: ");
        System.out.println("A.Arithmetic Series");
        System.out.println("B.Geometric Series");
        System.out.println("C.Largest prime number");
        System.out.println("Q.Quit the program");

        System.out.print("Enter your option: ");
        String option = sc.next();
        System.out.println("Your option is "+option);
        if(option.equals("A"))
        {
            System.out.println("You want to print an arithmetic series");
            System.out.print("Enter the number of terms to print: ");
            int number = sc.nextInt();
            System.out.print("Enter the first term of the series: ");
            int first_number = sc.nextInt();
            int series_value = first_number;
            System.out.print("Enter the common difference: ");
            int common_difference = sc.nextInt();
            System.out.println("Your arithmetic series is: ");
            System.out.print(series_value+",  ");
            for(int i = 0; i< (number-1); i++)
            {
                series_value = series_value+common_difference;
                System.out.print(series_value+",  ");
            }
        }
        else if(option.equals("B"))
        {
            System.out.println("You want to print an Geometric series");
            System.out.print("Enter the number of terms to print: ");
            int number = sc.nextInt();
            System.out.print("Enter the first term of the series: ");
            int first_number = sc.nextInt();
            System.out.print("Enter the common ratio: ");
            int common_ratio = sc.nextInt();
            System.out.println("Your Geometric series is: ");
            //int c = pow(2,1);
            for(int i = 0; i< number; i++)
            {

                System.out.print((first_number*(Math. pow(common_ratio,i)))+",  ");
            }
        }
        else if(option.equals("C"))
        {
            System.out.println("You want to print the largest prime number in a given range(inclusive): ");
            System.out.print("Enter the lower limit of the range: ");
            int lower_limit = sc.nextInt();
            System.out.print("Enter the upper limit of the range: ");
            int upper_limit = sc.nextInt();
            int highest_prime = 0;
            //let us define a method of checking whetjer a number is a prime number



            for(int i = upper_limit; i>(lower_limit-1);i--)
            {
                boolean isItPrime = checkForPrime(i);

                if (isItPrime)
                {
                    highest_prime = i;
                    break;
                }

            }
            //print the largest prime number
            System.out.println("The largest prime number in the given range is "+highest_prime);

        }
        else if(option.equals("Q"))
        {
            System.out.println("The program has been exited successfully");
        }
        else
        {
            System.out.println("Invalid option please try again");
        }

        //ask the user if he/she wants to perform another operation
            if(!option.equals("Q")) {
                System.out.println("\nDo you want to continue with another operation");
                System.out.println("Enter 1 for YES and any other integer for NO");
                int my_option = sc.nextInt();
                exit_condition = my_option;
            }
            else {
                exit_condition =0;
            }
        }

    }
}



SAMPLE PROGRAM OUTPUT



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