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

2
Contributors
1
2
Views
13 Years
Discussion Span
Last Post by jasweb2002

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.