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 01-Nov-2004, 14:53
quasimof quasimof is offline
New Member
 
Join Date: Oct 2004
Posts: 4
quasimof is on a distinguished road

prime numbers


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

CPP / C++ / C Code:
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:
CPP / C++ / C Code:
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.
Last edited by dsmith : 01-Nov-2004 at 16:44. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 01-Nov-2004, 20:35
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi quasimof. Your program is giving you an error, because the first time through your loop i is 0. You are dividing num by i and any number divided by 0 is... undefined.

Anyway, I don't think your logic is sound either. If you search these forums you should be able to find several threads that have good prime number finding algorithms such as this one here.

Once you get your prime number tester working, your other function should fall right into place

Good luck!
 
 

Recent GIDBlogAccepted for Ph.D. program 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
Help with random numbers da_bomb50 MySQL / PHP Forum 3 04-Aug-2004 20:34
Factorial of numbers cior C++ Forum 7 09-Jun-2004 21:08
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Help w/ prime # determination crystalattice C Programming Language 17 18-Apr-2004 22:43
Best way to validate numbers (ids)? JdS MySQL / PHP Forum 1 20-Jan-2003 04:55

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

All times are GMT -6. The time now is 01:24.


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