Results 1 to 12 of 12

Thread: prime numbers[Resolved by manavo11]

Threaded View

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved prime numbers[Resolved by manavo11]

    I was trying to create a program that printed all the prime numbers within a given limit. My program is printing out all of the numbers! See if you can figure out where I'm going wrong:

    Code:
    import java.io.*;
    
    class PrimeCheck
    {
    	public static void main(String[] args) throws Exception
    	{
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		System.out.print("Limit --> ");
    		int limit = Integer.parseInt(br.readLine());
    		System.out.println("");
    		System.out.print("Primes --> ");
    		int count = 0;
    		
    		for (int i=2; i<=limit; i++)
    		{
    			for (int j=1; j<=i; j++)
    			{
    				
    				if (j * i == i)
    				{
    					count++;
    				}
    				if (i * j == i)
    				{
    					count++;
    				}
    				if (count == 2)
    				{
    					System.out.print(i + " ");
    				}
    				count = 0;
    			}
    		}
    	}
    }
    Last edited by manavo11; Apr 3rd, 2005 at 02:58 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width