Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:15:35+00:00 2026-06-18T04:15:35+00:00

I am practicing past exam papers for a basic java exam, and I am

  • 0

I am practicing past exam papers for a basic java exam, and I am finding it difficult to make a for loop work for testing whether a number is prime. I don’t want to complicate it by adding efficiency measures for larger numbers, just something that would at least work for 2 digit numbers.

At the moment it always returns false even if n IS a prime number.

I think my problem is that I am getting something wrong with the for loop itself and where to put the “return true;” and “return false;”… I’m sure it’s a really basic mistake I’m making…

public boolean isPrime(int n) {
    int i;
    for (i = 2; i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

The reason I couldn’t find help elsewhere on stackoverflow is because similar questions were asking for a more complicated implementation to have a more efficient way of doing it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:15:37+00:00Added an answer on June 18, 2026 at 4:15 am

    Your for loop has a little problem. It should be: –

    for (i = 2; i < n; i++)  // replace `i <= n` with `i < n`
    

    Of course you don’t want to check the remainder when n is divided by n. It will always give you 1.

    In fact, you can even reduce the number of iterations by changing the condition to: – i <= n / 2. Since n can’t be divided by a number greater than n / 2, except when we consider n, which we don’t have to consider at all.

    So, you can change your for loop to: –

    for (i = 2; i <= n / 2; i++)  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm practicing my gui skills with Java and I have been doing menus and
I have started practicing TDD approach. I am pretty much new to unit testing.
practicing programming with MyProgrammingLab and getting following compile error: ApartmentBuilding.java:4: error: expected its also
I'm practicing C programming for Linux for an exam. I don't know how to
I am basically practicing with Java socket programming by building client and server (not
I'm practicing for an exam and trying to figure this out. I'm just not
Actually i had been practicing some of the java programs today(trying to learn it
I am practicing java in Eclipse both in home and college. Is there any
I was practicing writing macros and I can't seem to get defn to work.
Whilst practicing some TDD at work for an ASP.Net MVC project, I ran into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.