Practise Problem, Pair of Prime Numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Practise Problem, Pair of Prime Numbers

 
0
  #1
Sep 4th, 2008
Q.Write a program which will print all the pairs of prime numbers whose sum equals the number entered by the user. ( suggested by Aniseed ) (Intermediate)

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<math.h>
  4.  
  5. int prime(int);
  6. void main()
  7. {
  8. int i,j,c1,c2,num,f1,f2;
  9. clrscr();
  10. cout<<"Enter the number-";
  11. cin>>num;
  12. for(i=2,j=num-2;i<=num/2;i++,j--)
  13. {
  14. f1=prime(i);
  15. if(f1)
  16. {
  17. f2=prime(j);
  18. if(f2)
  19. cout<<"Pair:"<<i<<" + "<<j<<endl;
  20. }
  21. }
  22. getch();
  23. }
  24. int prime(int a)
  25. {
  26. int i;
  27. for(i=2;i<=sqrt(a);i++)
  28. if(a%i==0)
  29. return 0;
  30. else
  31. continue;
  32. return 1;
  33. }
Plz verify it. Any suggestions,comments or recommendations are welcomed.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Practise Problem, Pair of Prime Numbers

 
0
  #2
Sep 4th, 2008
> #include<iostream.h>
This is old. The correct form is #include <iostream>
If you (or your compiler) don't know about namespaces, then it's time you looked into it, or upgraded your compiler. namespaces have been standard issue since 1998, so there's really no excuse any more.

> #include<conio.h>
This is non-standard. If you can write your code without it, do so.

> #include<math.h>
In a namespace enabled compiler, this would be #include <cmath>

> void main()
This is just wrong. main returns an int.

Your prime() function could be more efficient, especially for large numbers.
How many even primes are there?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 26
Reputation: hiraksarkardg has a little shameless behaviour in the past 
Solved Threads: 2
hiraksarkardg hiraksarkardg is offline Offline
Light Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #3
Sep 4th, 2008
Salem's post is just OK. Things that I more wanna say that if you write #include<iostream> then you must write "using namespace std;" next line & you can use #include<cmath> in place of #include<math.h>. That's all.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #4
Sep 4th, 2008
I know about namespaces and the new standards i.e #include<iostream> , but ya my compiler "Turbo C++ v3.0" is old and dont understands the new formats like namespaces and the new style header files. An upgraded, according to the new standards, code will be

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. bool isprime(int);
  5. int main()
  6. {
  7. int i,j,c1,c2,num;
  8. bool f1,f2;
  9. cout<<"Enter the number-";
  10. cin>>num;
  11. for(i=2,j=num-2;i<=num/2;i++,j--)
  12. {
  13. f1=isprime(i);
  14. if(f1)
  15. {
  16. f2=isprime(j);
  17. if(f2)
  18. cout<<"Pair:"<<i<<" + "<<j<<endl;
  19. }
  20. }
  21. return 0;
  22. }
  23. bool isprime(int a)
  24. {
  25. int i;
  26. for(i=2;i<=sqrt(a);i++)
  27. if(a%i==0)
  28. return false;
  29. else
  30. continue;
  31. return true;
  32. }
And thanks Salem and hiraksarkardg for your concern. Is conio.h not ISO C++ standard. Which funtion to be used in place of clrscr() and getch()?
Last edited by vidit_X; Sep 4th, 2008 at 1:53 pm.
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Practise Problem, Pair of Prime Numbers

 
0
  #5
Sep 4th, 2008
> Is conio.h not ISO C++ standard.
Correct.

> Which funtion to be used in place of clrscr() and getch()?
Such things only matter (to some extent) when you're running from the IDE.
Most programs have neither.
Consider 'dir', which neither clears the screen, nor waits for a key press.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #6
Sep 4th, 2008
Why void main is deprecated in ISO standards? Where can i get a brief overview of the ISO C++ standards?
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Practise Problem, Pair of Prime Numbers

 
0
  #7
Sep 4th, 2008
You can't deprecate what was never standard to begin with.
void main has always been wrong, despite what many popular books and sloppy compilers say or do
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #8
Sep 4th, 2008
Thanks Salem for help, I think you really hate those who use "void main()" and you love "int main()" as your avtar and line under name says
Last edited by vidit_X; Sep 4th, 2008 at 3:19 pm.
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 23
Reputation: vidit_X is an unknown quantity at this point 
Solved Threads: 0
vidit_X vidit_X is offline Offline
Newbie Poster

Re: Practise Problem, Pair of Prime Numbers

 
0
  #9
Sep 5th, 2008
Any other tips for efficiency?
There is only 1 even prime number i.e 2.
Past:Beginner
Present:Intermediate
Future:Pro

The Future is near, as Daniweb members are so helpful and talented.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Practise Problem, Pair of Prime Numbers

 
0
  #10
Sep 5th, 2008
>>There is only 1 even prime number i.e 2

So, is there a simple way to determine if a number is even or odd? And, if any factor above 2 is even can the number be prime?

This might be a little more optimized:
  1. bool isprime(int a)
  2. {
  3. bool result = true;
  4. int i;
  5.  
  6. //assume a > 0
  7. if(a % 2 == 0)
  8. result = false;
  9. else
  10. {
  11. //check for factors, i, that are odd and above 1
  12. for(i = 3; i <= sqrt(a); i += 2)
  13. {
  14. if(a % i == 0)
  15. {
  16. result = false;
  17. break;
  18. }
  19. }
  20. return result;
  21. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC