binibiningtinamoran

MainApp

Nov 1st, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MainApp {
  4.  
  5.     public static void findOddAndPrimeNumbers(int low, int high) {
  6.  
  7.         for (int i = low; i <= high; i++) {
  8.             if (i % 2 != 0) {
  9.                 if (isPrime(i)) {
  10.                     System.out.printf("%,d is both odd and prime.\n", i);
  11.                 } else {
  12.                     System.out.printf("%,d is just an odd number.\n", i);
  13.                 }
  14.             } else {
  15.                 System.out.printf("%,d is an even number.\n", i);
  16.             }
  17.         }
  18.     }
  19.  
  20.     public static boolean isPrime(int entry) {
  21.         if (entry % 2 == 0) {
  22.             return false;
  23.         } else {
  24.             for (int j = 3; j*j <= entry; j += 2) {
  25.                 if (entry % j == 0) {
  26.                     return false;
  27.                 }
  28.             }
  29.         }
  30.         return true;
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.  
  35.         Scanner sc = new Scanner(System.in);
  36.         int lowerLimit;
  37.         int upperLimit;
  38.  
  39.         do {
  40.             System.out.print("\nEnter a lower limit of range: ");
  41.             lowerLimit = Integer.parseInt(sc.next());
  42.  
  43.             System.out.print("Enter a higher limit of range: ");
  44.             upperLimit = Integer.parseInt(sc.next());
  45.         } while (lowerLimit >= upperLimit);
  46.  
  47.         findOddAndPrimeNumbers(lowerLimit, upperLimit);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment