| | |
Practise Problem, Pair of Prime Numbers
![]() |
•
•
Join Date: Aug 2008
Posts: 23
Reputation:
Solved Threads: 0
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)
Plz verify it. Any suggestions,comments or recommendations are welcomed.
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> #include<math.h> int prime(int); void main() { int i,j,c1,c2,num,f1,f2; clrscr(); cout<<"Enter the number-"; cin>>num; for(i=2,j=num-2;i<=num/2;i++,j--) { f1=prime(i); if(f1) { f2=prime(j); if(f2) cout<<"Pair:"<<i<<" + "<<j<<endl; } } getch(); } int prime(int a) { int i; for(i=2;i<=sqrt(a);i++) if(a%i==0) return 0; else continue; return 1; }
> #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?
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?
•
•
Join Date: Aug 2008
Posts: 23
Reputation:
Solved Threads: 0
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
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()?
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<cmath> using namespace std; bool isprime(int); int main() { int i,j,c1,c2,num; bool f1,f2; cout<<"Enter the number-"; cin>>num; for(i=2,j=num-2;i<=num/2;i++,j--) { f1=isprime(i); if(f1) { f2=isprime(j); if(f2) cout<<"Pair:"<<i<<" + "<<j<<endl; } } return 0; } bool isprime(int a) { int i; for(i=2;i<=sqrt(a);i++) if(a%i==0) return false; else continue; return true; }
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.
Present:Intermediate
Future:Pro
The Future is near, as Daniweb members are so helpful and talented.
•
•
Join Date: Aug 2008
Posts: 23
Reputation:
Solved Threads: 0
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.
Present:Intermediate
Future:Pro
The Future is near, as Daniweb members are so helpful and talented.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
>>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:
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:
C++ Syntax (Toggle Plain Text)
bool isprime(int a) { bool result = true; int i; //assume a > 0 if(a % 2 == 0) result = false; else { //check for factors, i, that are odd and above 1 for(i = 3; i <= sqrt(a); i += 2) { if(a % i == 0) { result = false; break; } } return result; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: building Mysql on win XP Microsoft Visual Studio 9.0
- Next Thread: C vs C++ for a small application
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






