Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By jim829

Thread: Am I wrong in my prime no. program ?

  1. #1
    jasnoor.java is offline Member
    Join Date
    May 2016
    Posts
    2
    Rep Power
    0

    Unhappy Am I wrong in my prime no. program ?

    // Is anywhere my logic wrong ? ..
    // The aim is to find prime no.s from 1 to 100

    public class prime {
    public static void main (String[] args) {
    int x= 1;

    int count =0;
    for (x=1;x<101;x++) {

    for (int y=1 ; y<x+1; y++) {
    if (x%y==0) count++;
    }

    if (count==2) System.out.println (x + " is prime no.");
    }

    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Am I wrong in my prime no. program ?

    There are many short cuts to this process. However, you haven't explained what problems you are having. The one thing I notice is that you only
    print out something when count == 2. And please place your code between code tags. See BB Code List - Java Programming Forum - Learn Java Programming

    Regards,
    Jim
    jasnoor.java likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Am I wrong in my prime no. program ?

    Think of ir: every number can be divided by one, so there's no need to test it. Also, you're testing way too many possible divisors, i.e. for any number n and any number m, such that m*m <= n, if m divides n, then also n/m is a divisors of n. There are more rules to get the number of tries down ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    jasnoor.java is offline Member
    Join Date
    May 2016
    Posts
    2
    Rep Power
    0

    Default Re: Am I wrong in my prime no. program ?

    Hello all,

    Thank you for the help. I got the logic error which is : the count variable should be initialized in the first for loop, so that it automatically gets the value of 2 for each prime no. CHEERS !!!

    Thansks and regards,
    Jasnoor

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 07:40 AM
  2. Java program prime or not a prime number
    By xswtnsour13x in forum New To Java
    Replies: 2
    Last Post: 10-01-2013, 10:21 AM
  3. Check Prime numbers program
    By fallout87 in forum New To Java
    Replies: 1
    Last Post: 03-11-2012, 09:10 PM
  4. Palindrome Prime program
    By Zora in forum New To Java
    Replies: 7
    Last Post: 10-19-2011, 09:42 PM
  5. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 08:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •