package mypackage;


import java.util.Scanner;

public class twinPrimes {

    public static void main(String[] args){

        int userNum = 0;
        int attemptCount = 0;

        while (userNum < 3){
            System.out.println("Please enter an integer greather than three.");
            Scanner in = new Scanner(System.in);
            userNum = in.nextInt();
            attemptCount++;

            if (attemptCount == 3){
                System.exit(1);
            }
        }

        int count = userNum;

        for (int i = 3; i < count; i++){
            if (isPrime(i)){
                System.out.println(i + " is a Prime Number");
            }
        }


    }

    public static boolean isPrime(int num){
        boolean prime = true;
        int range = (num);

        for (int i = 3; i < range; i++ ){
            if (num % i == 0 ){
                prime = false;
                break;
            }

        }

        return prime;
    }




}

My code here reads a users input greather than or equal to three. (As per the assignment requirment). The only problem I am having is that when I run the code (Say the user enters 13), it returns 3 4 5 7 11. Every other number is correct, but I can't for the life of me figure out why it keeps returning a 4. Any tips/advice? Thanks!

for (int i = 3; i < range; i++ ){
    if (num % i == 0 ){
        prime = false;
        break;
    }
}

hint: for checking if the number is prime why does the condition here start with 3?

Ahh I see now, feels like a silly mistake when I see how minor of a change it is. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.