943,964 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4487
  • C++ RSS
Nov 1st, 2004
0

prime numbers

Expand Post »
i wrote this program to find prime numbers, but whenever i run it, i get an error.
This is the code [c++]:

int main(void)
{

int num=0;
int i=0;

cout<<"num";
cin>>num;

while(i<num)
{
num=num%i;
i++;
}
if(num==0)
cout<<"not prime";

else
cout<<"prime";

return 0;
}


My real task is to get the user to input a number greater than 2, and then print out what prime numbers added together would make up the number the user inputed. For example:

4 = 2 + 2
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7 etc.

i need to create a function: void Goldbach(int n, int& p1, int & p2)

and the main program would be:

void main()
{
int num, prime1, prime2;
cout<<"Enter an even positive integer greater than 2:";
cin>>num;
Goldbach(num,prime1,prime2);
cout<<num<<" = "<<prime1<<" + "<<prime2<<endl;

}

//my first step is to determine what 2 prime numbers would add up to get the //number the user inputs.
Edit/Delete Message
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
quasimof is offline Offline
4 posts
since Oct 2004
Nov 1st, 2004
0

Re: prime numbers

Quote originally posted by quasimof ...
int main(void)
{

int num=0;
int i=0;

cout<<"num";
cin>>num;

while(i<num)
{
num=num%i;
i++;
}
.
.
Not a master of the language but the error might be that you are dividing by zero. Not sure how C++ handles that off the top of my head.
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need help with an IF statement
Next Thread in C++ Forum Timeline: couple of bugs, suggestions welcomed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC