Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Find prime nums from 1-100 algorithm

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Find prime nums from 1-100 algorithm

    Hi to all.
    I learn java with some book and i tried to creat an algorithm that show the prime numbers form 1 to 100.
    The principle beahind it was that prime numbers can be divided by themself and 1 only - so the sum of the numbers that can be divided by is - the number himself plus 1.
    For example - 5 can be divided by 1 and 5 - so the sum of the dividers is 6...

    So this is the algorithm i created, and its say only 3 by the println..
    What is the problem please help me guys


    import java.util.Scanner;
    class Primery1
    {
    static Scanner reader = new Scanner(System.in);
    public static void main(String[] args)
    {
    int n, i, c ,sum =0;
       for (i=2; i<=100; i++)
       {
    	   for (n=1; n<=i; n++)
    	   {if ((i%n) == 0)
    		   sum=n+sum;
    	   }
    	   c=i+1;
    	   if (sum == c)
    	   System.out.println(i);}
     
     
     
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,101
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Find prime nums from 1-100 algorithm

    its say only 3 by the println..
    Can you show what you input to the program and what it prints out?
    How often is (sum == c)?
    Add a println to print out the value of c and the value of sum so you will see what the program is doing when it executes.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Find prime nums from 1-100 algorithm

    for (i=2; i<=100; i++)
       {
    	   for (n=1; n<=i; n++)
    	   {if ((i%n) == 0)
    		   sum=n+sum;
    	   }
    	   c=i+1;
    	   if (sum == c)
    	   System.out.println(i);} 
    }

    You have to restart the variable 'sum' in each for cycle because he's keeping the same sum as your previous number. In other words, the 'sum' variable is a total sum of all number's dividend from 1 to 100. You have to restart it like this:

    for (i=2; i<=100; i++)
       {
               sum = 0;
    	   for (n=1; n<=i; n++)
    	   {if ((i%n) == 0)
    		   sum=n+sum;
    	   }
    	   c=i+1;
    	   if (sum == c)
    	   System.out.println(i);} 
    }
    Last edited by Henry518; February 5th, 2012 at 10:41 PM.

Similar Threads

  1. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 12:05 AM
  2. trying to generate prime numbers
    By yingyang69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 28th, 2011, 11:21 AM
  3. prime numbers
    By tdz013 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2011, 10:24 AM
  4. composite and prime
    By cutee_eyeh in forum Object Oriented Programming
    Replies: 3
    Last Post: October 13th, 2010, 09:01 PM
  5. Need help.. Counting Prime #'s up to 50 w/while loop
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 6th, 2010, 05:40 PM