944,165 Members | Top Members by Rank

Ad:
  • Java Code Snippet
  • Views: 4932
  • Java RSS
0

PrimeNumbers.java

by on Jan 5th, 2009
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..
Java Code Snippet (Toggle Plain Text)
  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. }
Comments on this Code Snippet
Mar 1st, 2010
-1

Re: PrimeNumbers.java

can you create a code radix for ex: 1*10^6 + 2*10^5 + 3*10^4 +
4*10^3 + 5*10^2 + 6*10^1
coz i can't just get its iteration and slpiting the #s
Newbie Poster
atong is offline Offline
1 posts
since Mar 2010
Message:
Previous Thread in Java Forum Timeline: Its a simple programme for experinced but i m new in programming so plz help me .
Next Thread in Java Forum Timeline: Need help with arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC