+ 2

I want to print prime number between 1 and 10

when I run for code and enter tow numberes are 1,10 ,It output will be 1,2,3,4,5,6,7,8,9,10 Please correct code above to print that prime numberes are 2,3,7 #include <iostream> using namespace std; void prime( int m, int n) { for( int i=m; i<n; i++) { int p = 0 ; for ( int p= 2 ; p<m; p++) { if(i%p== 0 ) p=p+ 1 ; } if(p== 0 ) cout << i << endl; } } int main() { int m, n; cout << "enter two numbers" << endl; cin>>m>>n; prime(m, n); return 0 ; }

26th Feb 2017, 10:15 PM
Mohamed Abdalla
Mohamed Abdalla - avatar
3 Respostas
+ 2
If You need that to Your work - my code should be rght ffor You even though i use java. You gave me + for meant code.
26th Feb 2017, 11:22 PM
Michał Bujakowski
Michał Bujakowski - avatar
+ 2
I got done my version. https://code.sololearn.com/c0qKmPJ0vWHj/?ref=app Works fine.
27th Feb 2017, 2:03 AM
Michał Bujakowski
Michał Bujakowski - avatar
+ 1
Look into my code https://code.sololearn.com/cb2wiWxF6kG3/#cpp To fix your code replace prime function: void prime( int m, int n) { for( int i=m; i<n; i++) { if ( i == 1) continue; int f = 0 ; for (int p= 2 ; p<i; p++) { if(i%p== 0 ) f=1 ; } if(f== 0 ) cout << i << endl; } } or fix step by step: 1) add if ( i == 1) continue; - after line 6 to exclude 1 2) replace outer p with f - on line 8, 12, 14 3) fix condition p<m to p<i - on line 9 4) not necessary count p or f - on line 12
27th Feb 2017, 12:49 AM
Alexander Zakharov
Alexander Zakharov - avatar