Guest User

Untitled

a guest
Oct 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package crisostomolab3b;
  7. import java.io.*;
  8.  
  9. /**
  10. *
  11. * @author arscariosus
  12. */
  13. public class PrimeNumbers {
  14.  
  15. /**
  16. * @param args the command line arguments
  17. */
  18. public static void main(String[] args) throws Exception {
  19. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  20. int a, b, hit = -1;
  21. int choice;
  22. String confirm;
  23. char answer = 'y';
  24.  
  25. menu:
  26. do {
  27. System.out.println("\t\t\tMENU");
  28. System.out.println("[1]\tPrime or Composite");
  29. System.out.println("[2]\tAll Prime Numbers");
  30. System.out.println("[3]\tExit");
  31. System.out.print("Enter your choice : ");
  32. choice = Integer.parseInt(input.readLine());
  33.  
  34. switch(choice) {
  35. case 1: System.out.print("Please enter a number : ");
  36. a = Integer.parseInt(input.readLine());
  37. for(int i = 2;i < a; i++) {
  38. if(a % i == 0) {
  39. hit = 1;
  40. }
  41. }
  42. if(hit < 0) {
  43. System.out.println(a + " is a prime number.");
  44. } else {
  45. System.out.println(a + " is a composite number.");
  46. }
  47. hit = -1;
  48. break;
  49.  
  50. case 2: System.out.print("Please enter the first number : ");
  51. a = Integer.parseInt(input.readLine());
  52. System.out.print("Please enter the second number : ");
  53. b = Integer.parseInt(input.readLine());
  54. if (a > b) {
  55. int temp = a;
  56. a = b;
  57. b = temp;
  58. }
  59. System.out.print("Prime numbers between " + a + " and " + b + " are : ");
  60. for(int i = a; i <= b; i++) {
  61. for(int j = 2; j < i; j++) {
  62. if(i % j == 0) hit = 1;
  63. }
  64. if(hit < 0 && i >= -1 && i != 1 && i != 0) System.out.print(i + " ");
  65. hit = -1;
  66. }
  67. System.out.println();
  68. break;
  69. case 3: System.out.print("Are you sure? (yes/no)");
  70. confirm = input.readLine();
  71. if(confirm.equals("yes"))
  72. break menu;
  73. else
  74. continue menu;
  75. default: System.out.println("Invalid input!");
  76. }
  77.  
  78. ask:
  79. System.out.print("Would you like to try another?");
  80. answer = (char) System.in.read();
  81. System.in.read();
  82. }while(answer == 'y');
  83. }
  84. }
Add Comment
Please, Sign In to add comment