GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2009, 01:03
disk97 disk97 is offline
New Member
 
Join Date: Mar 2009
Posts: 13
disk97 has a little shameless behaviour in the past

Create a program that computes all the prime numbers less than 10,000


Create a program that computes all the prime numbers less than 10,000 by storing them in an array and then printing them out, 10 primes to a row. (A prime is a positive integer greater than 1 whose only factors are 1 and itself).

(a)To test if a positive integer N is prime, one can divide N by all integers from 2 up to the square root of N.
As soon as you find one factor of N, you can stop.
If you get all the way to without finding a factor, then N is prime.

(b)write the program in the more efficient manner where you divide N by all primes less than or equal to , stopping if and when you find a factor.
This will require you to store the primes in an array as you compute them, and then for each integer N check to see if any elements of the array are factors of N.
This comparison will be inside a “for” or “while” loop.

c)You may enter the first element of the array, say prime[1] = 2, directly in the program.
------------------------------------------------------------------
I want to print all prime number from 2 to 10000.
10 primes to row.

my code is below

CPP / C++ / C Code:
#include <iostream>
#include<iomanip>

using namespace std;

main ()
{
	int i,j,prime[10000],count=0;

	bool flag=true;

	for (i=2;i<=10000;++i)
	{
		for (j=2;j<=sqrt(i*1.0);++j)
			if(i%j==0)
				flag=false;
		if (flag)
		{
		prime[count]=i;
		count++;
		}
	for (i=0;i<=10000;++i)    // print 10 primes to row
		{
			cout<<setw(7)<<i;
			if (i%10==9)
				cout<<endl;
		}
	}
	cout<<endl;
	cout <<"\t"<<"Program shows "<< count-1 << " all prime numbers";
	cout<<" from 2 to 10000."<<endl;
}
Last edited by admin : 03-Apr-2009 at 01:25. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 03-Apr-2009, 10:25
zalezog zalezog is offline
Junior Member
 
Join Date: Oct 2007
Posts: 33
zalezog will become famous soon enough

Re: Create a program that computes all the prime numbers less than 10,000


A few braces here and there and the whole program went haywire.
CPP / C++ / C Code:

for (i=2;i<=10000;++i)
	{
		 flag  = true ;//You need to assign again after 
                                   //each iteration,else will remain static           
                                   //(false)  throughout,
            //naming flag  as is_prime or something  of that sort 
           //might have been better.        
                             for (j=2;j<= (int)sqrt(i*1.0);++j) 
                                  //comparing int with a double value?,need a cast
	               {
                                   if(i%j==0)
	                    {			
                                    flag=false;
		                        break ; 
                                         //you have found one of your factors.
                                         //Why to  continue further..
                                 }
                            
                            } // j loop ends
                 
                           if (flag)
		{
		prime[count]=i;
		count++;
		}
	}  // i loop ends
                
                            for (i=0;i< count;++i)    // You have found 'count prime 
                         //numbers not  10,000
		{
			cout<<setw(7)<<i;
                //>>>While you store your prime numbers in prime[],
               //you display all the 'i' 's in the for loop,should be prime[count]
			
                                                       if (i%10==9)
				cout<<endl;
		}
	//}should be above


  #3  
Old 03-Apr-2009, 12:27
disk97 disk97 is offline
New Member
 
Join Date: Mar 2009
Posts: 13
disk97 has a little shameless behaviour in the past

Re: Create a program that computes all the prime numbers less than 10,000


The code does not run. I fix my code but it still show one error.

Compiler error is that

Code:
Warning W8004 assign8.cpp 13: 'is_prime' is assigned a value that is never used in function main()

The code is

CPP / C++ / C Code:

#include <iostream>
#include<iomanip>

using namespace std;

main ()
{
	int i,j,prime[10000],count=0;
	bool is_prime=true;

	for (i=2;i<=10000;++i)
	{
		is_prime=true;
		for (j=2;j<=sqrt(i*1.0);++j)
		{
			if(i%j==0)
			{
				is_prime=false;
				break;
			}
		}
		if (is_prime)
			{
			prime[count]=i;
			count++;
			}
	}
	for (i=0;i<=count;++i)    // print 10 primes to row
		{
			cout<<setw(7)<<prime[count];
			if (i%10==9)
				cout<<endl;
		}
	cout<<endl;
	cout <<"\t"<<"Program shows "<< count-1 << " all prime numbers";
	cout<<" from 2 to 10000."<<endl;
}

  #4  
Old 03-Apr-2009, 12:54
zalezog zalezog is offline
Junior Member
 
Join Date: Oct 2007
Posts: 33
zalezog will become famous soon enough

Re: Create a program that computes all the prime numbers less than 10,000


Please use code tags next time.

Please indent your code.
You also need to include


Code:
#include<cmath> //for sqrt function
Quote:
'is_prime' is assigned a value that is never used
in function main()"

I don't get this message.
  #5  
Old 03-Apr-2009, 18:16
disk97 disk97 is offline
New Member
 
Join Date: Mar 2009
Posts: 13
disk97 has a little shameless behaviour in the past

Re: Create a program that computes all the prime numbers less than 10,000


I will use cod tags next time.
thank you very much! I got it
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Guidelines for posting requests for help - UPDATED! WaltP C Programming Language 1 11-Nov-2007 22:05
Barcode reader code problem copa_21 C Programming Language 5 13-Oct-2006 01:40
User defined headers davis Miscellaneous Programming Forum 6 16-Feb-2006 19:40
Could Someone Please Explain This Code To Me HelpMePlease C Programming Language 3 26-May-2004 23:46
something wrong with this code loon MySQL / PHP Forum 5 07-Jul-2003 06:55

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:22.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.