javalogic-error

Why is my program to find prime numbers within a range not working the way it's supposed to


I tried creating a program to find prime numbers within a range. The user is asked to input two numbers then the goal of the program will be to print out the prime numbers between the first number and the last number.

Here's my code below:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // program to check prime numbers b/w two numbers
        Scanner sc = new Scanner(System.in);
        byte factorsCount = 0;

        System.out.println("Enter first number: ");
        int numOne = sc.nextInt();
        System.out.println("Enter second number: ");
        int numTwo = sc.nextInt();

        System.out.println("The prime numbers are: ");

        for (int i = numOne+1; i < numTwo; i++) {
            for (int j = 1; j <= i; j++) {
                if (i%j == 0) {
                    factorsCount++;
                }
            }
            if (factorsCount == 2) {
                System.out.println(i);
            }
        }
    }
}

The problem is that the program is not working as intended. For some numOne and numTwo it prints some prime numbers whilst for others it prints nothing

The way I intended for it to work is as follows:

  1. It asks the user for the first number and the second number. It then creates a variable to store the no. of factors a specific number has
  2. The outer for loop iterates through the numbers between the first and second number
  3. The inner for loop counts the number of factors a number has
  4. After the inner loop is done, it breaks out of the inner loop then checks if the factorsCount is equal to 2. if yes, then it is a prime number hence it prints it out and if no it just goes to the next number within the range
  5. It repeats steps one through four until it has exhausted the numbers within the Range after which it must have printed all the prime numbers

Solution

  • Here is a working copy of your code

    package com.goodyear;
    
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // program to check prime numbers b/w two numbers
            Scanner sc = new Scanner(System.in);
            byte factorsCount = 0;
    
            System.out.println("Enter first number: ");
            int numOne = sc.nextInt();
            System.out.println("Enter second number: ");
            int numTwo = sc.nextInt();
    
            System.out.println("The prime numbers are: ");
    
            for (int i = numOne+1; i < numTwo; i++) {
                for (int j = 1; j <= i; j++) {
                    if (i%j == 0) {
                        factorsCount++;
                    }
                }
                if (factorsCount == 2) {
                    System.out.println(i);
                }
                factorsCount = 0;
            }
        }
    }
    

    Resetting the factorCount variable after evaluation allows for the next evaluation to occur as designed. Also, consider making you evaluation inclusive on numOne and numTwo by changing your loop variables.