+ 1

What's error in this code?

I've written code for finding prime numbers between two numbers. But it's not giving the output as prime numbers; and also at end, it's showing something which I am unable to understand. Forgive me if I am asking silly question (I am just beginner) Edit: This question is solved now and I made corrections in code. Thanks to all who helped! https://code.sololearn.com/cfV7F2mKY3G1/?ref=app

12th Dec 2020, 3:33 PM
Shireen
Shireen - avatar
6 Respuestas
+ 1
Shireen #include <iostream> #include <math.h> using namespace std; bool isPrime(int z){ for(int i=2; i<= z / 2; i++){ if(z % i == 0) { return false; } } return true; } int main() { int a; int b; cin>>a>>b; for(int i=a; i<=b; i++){ if(isPrime(i)) cout<<i<<endl; } return 0; }
12th Dec 2020, 4:05 PM
A͢J
A͢J - avatar
+ 1
Thanks I am Groot! Now I've understood my mistake. And also..thanks Azatgylych(But your code not worked. May be I've done mistake, I don't know)
12th Dec 2020, 4:21 PM
Shireen
Shireen - avatar
0
Shireen Always remember that you have to write return statement at the end of the method. The error is coming because of that. 2nd thing never write return statement in else part inside loop because next iteration will not work.
12th Dec 2020, 3:48 PM
A͢J
A͢J - avatar
0
I've written return 0; can you explain which return statement?
12th Dec 2020, 3:50 PM
Shireen
Shireen - avatar
0
Shireen You have returned 0 inside main method but what about isPrime method
12th Dec 2020, 3:51 PM
A͢J
A͢J - avatar
0
Oh thanks... understood...And also prime numbers are not printing... it's just giving odd numbers as output instead of prime numbers... how to correct that
12th Dec 2020, 3:53 PM
Shireen
Shireen - avatar