Question
write a java program A prime number is a number that has two factors: 1 and itself. For example, the first 5 prime numbers are:
write a java program
A prime number is a number that has two factors: 1 and itself. For example, the first 5 prime numbers are: 2,3,5,7,11.
Write a program to read a positive non-zero even number from the keyboard.
Print the last 5 prime numbers before the entered value and 3 prime numbers after the entered value. The length of the generated prime numbers is always 8. For example, the user entered 44 then the output in this case is
29 31 37 41 43 47 53 59
Another example the user entered 20
7 11 13 17 19 23 29 31
There is a possibility if the entered value may have less than 5 prime numbers before that. In this case, then the extra prime numbers are printed after the entered number to ensure the length is always 8.
For example, the user entered 6, there are only 3 prime numbers before 6. There are two missing prime numbers. So, in this case two extra prime numbers are printed after 6 to ensure the length is always 8. The solution in this case is
2 3 5 7 11 13 17 19
Create two classes to generate the Prime numbers. They are primeNumberBack and primeNumberForward. The class primeNumberBack will generate the prime numbers before the entered number. For example, if the entered number is 42 then in this class the following prime numbers will be generated: (23 29 31 37 41) while the class primeNumberForward, in this case, will generate the remaining three prime numbers (43 47 51) to conform to the length of 8.
In the main method, the user enters the number, and accordingly will call the respective classes to generate the prime numbers, the final output result is printed in the main method.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started