Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Version 10/15/2019
- * Description: This program displays all the prime numbers up to 50 numbers
- *
- * Pseudocode:
- * 1. Create a counter variable and number variable to store prime number value
- * 2. check if the number is prime when incrementing
- * 3. display the number if prime
- */
- import java.util.Scanner;
- public class DisplayingPrimeNumbers {
- public static void main (String[] args) {
- System.out.println("ID002");
- final int NUMBER_OF_PRIMES = 50,
- NUMBER_OF_PRIMES_PER_LINE = 10;
- int count = 0, number = 2;
- System.out.println("The first 50 prime numbers are \n");
- //find prime numbers iteratively
- while (count < NUMBER_OF_PRIMES) {
- boolean isPrime = true;
- //test whether the number is prime
- for (int divisor = 2; divisor <= number / 2; divisor++) {
- if (number % divisor == 0) {
- isPrime = false;
- break;
- }
- }
- //Display the prime num and increment the count
- if (isPrime) {
- count++;
- if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
- //display num and proceed to next line
- System.out.println(number);
- }
- else {
- System.out.print(number + " ");
- }
- }
- number++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement