Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. /*
  2. * Version 10/15/2019
  3. * Description: This program displays all the prime numbers up to 50 numbers
  4. *
  5. * Pseudocode:
  6. * 1. Create a counter variable and number variable to store prime number value
  7. * 2. check if the number is prime when incrementing
  8. * 3. display the number if prime
  9. */
  10.  
  11. import java.util.Scanner;
  12. public class DisplayingPrimeNumbers {
  13. public static void main (String[] args) {
  14. System.out.println("ID002");
  15.  
  16. final int NUMBER_OF_PRIMES = 50,
  17. NUMBER_OF_PRIMES_PER_LINE = 10;
  18.  
  19. int count = 0, number = 2;
  20.  
  21. System.out.println("The first 50 prime numbers are \n");
  22.  
  23. //find prime numbers iteratively
  24. while (count < NUMBER_OF_PRIMES) {
  25. boolean isPrime = true;
  26.  
  27. //test whether the number is prime
  28. for (int divisor = 2; divisor <= number / 2; divisor++) {
  29. if (number % divisor == 0) {
  30. isPrime = false;
  31. break;
  32. }
  33. }
  34.  
  35. //Display the prime num and increment the count
  36. if (isPrime) {
  37. count++;
  38. if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
  39. //display num and proceed to next line
  40. System.out.println(number);
  41. }
  42. else {
  43. System.out.print(number + " ");
  44. }
  45. }
  46. number++;
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement