Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name:
- * Date:
- * Course Number:
- * Course Name:
- * Problem Number:
- * Email:
- * Short Description of the Problem
- */
- import java.util.Scanner;
- import java.lang.Math;
- public class UglyNumberTester {
- //**********************************************
- private static void input(Scanner sc, String args[]) {
- int x;
- int y;
- System.out.print("Enter starting value: ");
- x = sc.nextInt();
- sc.nextLine();
- System.out.print("Enter end value: ");
- y = sc.nextInt();
- sc.nextLine();
- System.out.println( process(x,y) );
- sc.nextLine();
- }
- private static String process(int x, int y) {
- String output = "";
- while ( x <= y ) {
- if ( isUgly(x))
- {
- output += " " + x;
- }
- x++;
- }
- return output;
- }
- //checks if the number has prime factors of only 5,3 or 2
- private static boolean isUgly(int z) {
- while(z % 5 == 0)
- {
- z /=5;
- }
- while(z % 3 == 0)
- {
- z /=3;
- }
- while (z % 2 == 0)
- {
- z /=2;
- }
- if(z >= 7)
- {
- return false;
- }
- return true;
- }
- //**********************************************
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- //**********************************************
- public static void main(String args[]) {
- final String TITLE = "Ugly Number Tester";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- input(sc, args);
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Add Comment
Please, Sign In to add comment