Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created September 13, 2021 20:40
Show Gist options
  • Save jananpatel2002/6d3c277f5caf6ee6b3aaec594a9dee1d to your computer and use it in GitHub Desktop.
Save jananpatel2002/6d3c277f5caf6ee6b3aaec594a9dee1d to your computer and use it in GitHub Desktop.
/*
* Name: Janan Patel
* Date: 9/13/2020
* Course Number: 220
* Course Name: Data Structures
* Problem Number: 2
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Summing all prime numbers in given values
*/
import java.util.Scanner;
public class PrimeNumbersAnalysis {
private final static String TITLE = "Prime Numbers Analysis V1.0";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
// **********************************************
// Put as many methods you need here
public static boolean isPrime(int number) {
for (int divisor = 2; divisor <= (int) Math.sqrt(number); divisor++) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
public static void printPrimeNumbers(int min, int max) {
int sum = 0;
while (min <= max) {
if ((isPrime(min) && isPrime(reverse(min))) && (isPal(min) == false)) {
sum = sum + min;
min++;
} else
min++;
}
System.out.println("The total for the sum inbetween those two points is" + " = " + sum);
}
public static int reverse(int number) {
int reversed = 0;
while (number != 0) {
int remainder = number % 10;
reversed = reversed * 10 + remainder;
number = number / 10;
}
return reversed;
}
public static boolean isPal(int number) {
if (number == reverse(number)) {
return true;
} else
return false;
}
// **********************************************
// Start your logic coding in the process method
private static void process(Scanner sc, String args[]) {
System.out.print("Enter your starting point: ");
int min = sc.nextInt();
System.out.print("Enter your ending point: ");
int max = sc.nextInt();
printPrimeNumbers(min, max);
sc.nextLine(); // Clear Keyboard
}
// **********************************************
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
}
// **********************************************
// Do not change the main method
public static void main(String args[]) {
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment