### Need help

List all the prime numbers between M and N.

input
2 5
Output
2
3
5

closed account (EwCjE3v7)
Can you show us your code? I don't know what your question is besides that the output is wrong. Show us the code and we can help. How can we help you if we don't know what your even talking about.
This is the question...
Try to write a code!
This is very easy in C# (but not in C++). So here is a C# solution:

 ``123456789101112131415161718192021222324252627282930313233`` ``````using System; public class Primes { static void Main() { string input; int M, N; input = Console.ReadLine(); string[] input_mn = input.Split(' '); M = Convert.ToInt32(input_mn[0]); N = Convert.ToInt32(input_mn[1]); for(int i = M; i<=N; i++) { int lim = (int)Math.Sqrt(i); int divs = 0; for(int j = 2; j <= lim; j++) { if(i%j == 0) { divs++; } } if(divs == 0 && i != 1) { Console.WriteLine(i); } } } }``````
Last edited on
I need the code in c++...
At least creating array with prime numbers!
Well, there's no need for an array.
As a first step, create a loop which will simply output all the numbers in the range M to N. When you have that working, add a test to determine whether or not each number is prime, and thus either print or ignore that number.

The previously posted code at least gives you an overview of the sort of logical flow which is required - though some of it would be simpler in C++.
Last edited on
Try it yourself first Albo Coder. What have you done so far?
 ``12345`` ``````#include bool \$\$(unsigned int __){unsigned int _; for(_=2;_*_<__;_++)if(!(__%_))return 0;return _*_!=__;} int main(){unsigned int _,__;std::cin>>_>>__; for(unsigned int \$=_;\$<=__;\$++)if(\$\$(\$))std::cout<<\$<<'\n';return 0;}``````
Last edited on
Since you need it badly, here:

(Its basically a translation of my C# code)

 ``1234567891011121314151617181920212223242526`` ``````#include #include using namespace std; int main() { int m, n; cin >> m >> n; for(int i = m; i<=n; i++) { int divs = 0; int lim = (int)sqrt(i); for(int j = 2; j<=lim; j++) { if(i%j == 0) {divs++;}; } if(divs == 0 && i != 1) { cout << i << endl; } } return 0; }``````
http://www.cplusplus.com/forum/beginner/1/#msg6680

Remember, this being homework, @Albo Coder should at least give some indication that he has tried it himself, otherwise how is anyone supposed to learn?
@Chervil
You are right!
The problem is that I don't know any algorithm to find the prime numbers...
I need the code!
@Stormboy
I need it in a contest!
AND THANKS!!!
Topic archived. No new replies allowed.