| Bollom (3) | |
|
Hi guys, i have some problems with my program my program should ask user ton enter two numbers and then generate the primes between these numbers and output it on screen and i must use a void function as our doctor say unless i will lose marks i could not make the function void so i made it return function and after i wrote the program i discovered that the program work correctly but it is output the primes and after the primes directly output " 1 " so i have two problems the first that i must convert the function to void and the second is i want to remove the " 1 " which make me crazy because its appear in every output and i hope you help me guys , this is the program : #include<iostream> #include<cmath> using namespace std; printPrimes ( int, int); int main() { int num,num2; cout<<"Enter The First Number:"<<endl; cin>>num; cout<<"Enter The Second Number:"<<endl; cin>>num2; while (num>=num2) { cout<<"The First Number Should Be smaller Than The Second Number"<<endl; cout<<endl; cout<<"Enter The First Number"<<endl; cin>>num; cout<<"Enter The Second Number"<<endl; cin>>num2; } cout<<"The Primes Between The First Number and The Second Number are"<<endl; cout<<printPrimes (num,num2)<<endl; return 0; } printPrimes (int num,int num2) { bool prime = true; int number3; number3 =(int) floor (sqrt (num2)); while (num<=num2) { for ( int j = 2; j<=number3; j++) { if ( num!=j && num % j == 0 ) { prime = false; break; } } if (prime) { cout <<" "<<num<<" "<<endl; } prime = true; num++; } return prime; } and thanks in advance. | |
|
|
|