Thread: Help with my program please :)

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    Help with my program please :)

    Anyways I am trying to create a program that counts all the prime numbers between 1 and 500. Its executing with no errors or warnings but there is no output
    Code:
    #include <iostream.h>
    //this program is made to count all 
    //the prime numbers between 1 and 500
    int main()
    	{
    		int i=1;
    		int n=2;
    		int prime=0;
    
    		for(i=1;i<500;i++)
    		{
    			for(n=2;n<500;n++)
    			{
    				if (i%n==0) break;
    			}
    			if ( n==(i-1) )
    				{
    					cout << i; // all the prime numbers
    					cout << prime; //number of primes
    					prime++;
    				}
    		}
    	return 0;	
    }
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    dont u need to create a function which will output ur result on the screen?

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
    			for(n=2;n<500;n++)
    			{
    				if (i%n==0) break;
    			}
    			if ( n==(i-1) )
    				{
    					cout << i; // all the prime numbers
    					cout << prime; //number of primes
    					prime++;
    				}
    should be:
    Code:
    			for(n=2;n<500;n++)
    			{
    				if (i%n==0) break;
    				if ( n==(i-1) )
    				{
    					cout << i; // all the prime numbers
    					cout << prime; //number of primes
    					prime++;
    				}
    			}

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    Thanks for the help guys, I worked on it a bit more and came up with:
    Code:
    #include <iostream.h>
    //this program is made to count all 
    //the prime numbers between 1 and 500
    int main()
    	{
    		int i=1;
    		int n=2;
    		cout << 2 << " ";
    		for (i = 1; i < 500; i++)
    		{
    		for (n = 2; n < (i - 1); n++)
    		{
    			if (i % n == 0)
    			{
    				break;
    			}
    		}
    		if (n == (i - 1))
    		{
    			cout << i << " ";
    		}
    	}
    	return 0;	
    }
    well a friend helped :]
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >if (n == (i - 1))
    The if statement is redundant. You don't need it at all.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    hey i tryed ur code too with the correction jlou posted i added the following
    Code:
    cout << n; // all the numbers
    cout << "\nprime: " <<i<<"\n"; //all primes
    that works too
    Last edited by InvariantLoop; 04-21-2004 at 04:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM