PrimeNumbers.java

code_warrior code_warrior is offline Offline Jan 5th, 2009, 10:22 pm |
0
This code enables the user to enter a number and finds the prime from 2 to the entered number..Please name it PrimeNumbers.java Hope it'll help u..
Quick reply to this message  
Java Syntax
  1. /*
  2. creator: jennyvi San sebastian
  3.  
  4.  
  5.  
  6.  
  7. */
  8.  
  9.  
  10. import java.util.Scanner;
  11.  
  12.  
  13. public class PrimeNumbers{
  14. public static void main(String ivy[]){
  15.  
  16. int input = 0;
  17. int counter;
  18. int div;
  19. String choice = null;
  20.  
  21. do{
  22. Scanner keyIn = new Scanner(System.in);
  23. System.out.print("Enter a number: ");
  24. input = keyIn.nextInt();
  25.  
  26. int start = 2;
  27.  
  28. while (start <= input){
  29. div = 2;counter = 0;
  30.  
  31. while (div <= start)
  32. {
  33. if (start % div == 0 && div==start)
  34. {
  35. counter++;
  36. }
  37. if (start % div == 0 && div!=start)
  38. {
  39. counter++;
  40.  
  41. }
  42.  
  43. div++;
  44. }
  45.  
  46. if (counter == 1)
  47. {
  48. System.out.print(start + " ");
  49. }
  50.  
  51. start++;
  52. }
  53.  
  54. System.out.println();
  55.  
  56. System.out.println("\nDo you want to continue <YES/N0>: ");
  57. choice = keyIn.next();
  58. choice = choice.toUpperCase();
  59.  
  60. }while(choice.equals("YES"));
  61.  
  62. }

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC