You are using a version without Ads of this website. Please, consider donating:

### find the prime numbers between two values

i did a code that determine if the number is prime or not, and i have to do a one that finding the prime numbers between two variables .

the first code:

#include<iostream>
using namespace std;
int main(){
int m,n;
bool y=true;
cout<<"the number : ";
cin>>m;
for(int i=2;i<m;i++){
if(m%i==0)
y=false;
}
if (y==true)
cout<<"the number is prime "<<endl;
else if(y==false)
cout<<"the number is not"<<endl;

system("pause");
return 0;
}

but the second code there is wrong with it but i couldn't find it,any help!!

the second code :
#include<iostream>
using namespace std;
int main(){
int m,n;
bool y=true;
cout<<"the number : ";
cin>>m>>n;

for(int k=1;m=n;k++){

for(int i=2;i<m;i++){
if(m%i==0)
y=false;
}
if (y==true)
cout<<"the number is prime "<<endl;
else if(y==false)
cout<<"the number is not"<<endl;
m--;
}

system("pause");
return 0;
}

Please explain what the problem is. How do you know something is wrong with it?

I notice, however, that the first for loop in your second program has an assignment in the condition section; you probably meant to do an equality comparison.
use the code tags, eases reading

your first for loop does nothing

`for(int k=1;m=n;k++){`

your code needs more changes, but Im going to assume you want to find the primes between M and N, two input numbers.

your first loop would look like
`for(int k=m; k<=n; k++){`
this sets the start of the loop at the first integer (m) and runs until it reaches the second integer (n) increasing by one each time.
then within this loop you have another loop `for(i = 2, i < k; i++)`, iterate between every number from 2 to k-1 and checking if the expression m%i == 0, if it comes up true, break out of the loop and start over the first loop again, if it doesnt come up true and the inner loop ends, add the current k to an array or a string, and then continue checking through the numbers

once the loops end you can then output the string or char array holding your prime numbers.

I like the thought of just checking for numbers between 2 and n-1.. I made a similar program but I checked from 1 to the number itself, but I did expand it to check for any number of factors.. it does only check from 1-1000, but adding in the functionality to check for numbers with a given number of factors within a given range would be easy.. heres the code to mull over if you like:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````//Finds numbers between 1 and 1000 with a given number of factors #include #include #include #include using namespace std; inline string toStr(int a){ return static_cast(&(ostringstream()<str(); } int main() { while(true){ string outStr; int factors; int totalNums = 0; cout<<"Find numbers between 1 and 1000 with how many factors?"<>factors; cout<::max(), '\n'); cout<<"That's not a number! :(\n\n"; continue; } outStr = "Numbers with a total of " + toStr(factors) + " factors:\n\n"; //begin iterating through numbers for(int i = 1; i < 1000; i++){ int numEvenDivs = 0; //iterate through possible factors for(int j = 1; j <= i; j++){ if(i%j == 0){ //if there are more factors than we're looking for, break out of loop if(++numEvenDivs > factors) break; } } //display number and its factors if(numEvenDivs == factors){ ++totalNums; outStr += "> " + toStr(i) + ": "; for(int k = 1; k <= i; k++){ if(i%k == 0){ outStr += toStr(k)+ " "; } } outStr += "\n"; } } //if there arent any numbers at all if(totalNums == 0){ cout<<"No such numbers exist!\n\n"; continue; } //final output to console cout<>getResults; if(getResults == 'Y' || getResults == 'y'){ ofstream out(string(toStr(factors) + "-factors.txt").c_str()); out<

note im just a beginner too so im sure theres some not so "proper" stuff in there.. not sure shrug! youll notice i do a second loop to output the factors after I know a number has the given number of factors im looking for, this seems at first, but otherwise I would have to keep note of each factor as I went along, whether the current number ended up with more or less than the number of factors I wanted.. I guess I could have just had a 'buffer' stirng to hold this information and then if it checked out append it to the output string and then clear the buffer.. but this seemed like it might be less work overhead..

goodluck

Last edited on
thank you very much but i study just if conditions and looping not reached to string and array...
i think my problem in the first loop..
 ``12345678910111213141516171819202122232425`` ``````#include int main() { using namespace std ; int m , n ; cout << "the two numbers : " ; cin >> m >> n ; for( int k = m ; k <= n ; ++k ) // for each number k in {m, m+1, ... n} { bool y = true ; for(int i = 2 ; i < k ; ++i ) // check if k is prime { if( k%i == 0 ) y = false ; } cout<< "the number " << k ; if(y) std::cout << " is prime\n" ; else std::cout << " is not prime\n" ; } }``````
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: