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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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++...
Please help me!
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?
1
2
3
4
5
#include <iostream>
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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>

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.