Need help with a program

So a question that came in my exam was "Write a program to list the prime numbers from m to n" I tried making the program right now but I manage to input the values but the output comes out to be blank.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int i,j,m,n,ctr;
 cout<<"enter the value of m and n respectively"<<endl;
 cin>>m>>n;
 for(i=m;i<=n;i++)
	{
         ctr=0;
	 for(j=1;j<=i;j++)
		{
		 if((i%j)==0)
		 ctr++;
                 }
         if(ctr==2)
                 {
                    cout<<i<<", ";
                 } 
	}
  getch();
}


I can't seem to find the error, whether it is logical or syntatical. I would really like some help, now Im really a beginner so I'd like if you could make it using for, if, cin, cout statements without any of those functions or print/scanf statements
Last edited on
There is a bug on line 17.

you have
cout<<i<<", "<<;

you should have
cout<<i<<", ";

You also need to initialize you ctr variable before using it.
Last edited on
i fixed the code a bit
 
int i,j,m,n,ctr=0;


also
 
cout<<i<<", ";


also
1
2
3
4
5
if(ctr==2)
     {
       cout<<i<<", ";
        ctr=0;
       }



edit:tried running it, i input the values but it returns nothing
Last edited on
You must reset ctr for every i. So instead of putting ctr=0 in you if (ctr==2);
write it at the begining of your i loop:

for(i=m; i<=n; i++) {
ctr = 0;
.....

}
I tried that but I still get a blank output with some weird error

OUTPUT:-

Enter the value of m and n respectively
5

20

Process returned 13 <0xD> execution time : 7.594 s
Press any key to continue.
I think you're using windows, if I'm wrong tell me, but I'm using Linux... things can be a bit different on the two OS. So, my adice would be to print out m and n to make sure that they are set to what you want.

tried it again and it worked, thanks. CLOSED
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
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <conio.h>

using namespace std;

int main ()
{
    unsigned int m, n, test;
    
    cout << "Please enter your first number: ";
    cin >> m;
    cout << "Please enter your second number: ";
    cin >> n;
    
    for (m; m <=n; m++)
    {
        test = 0;//this variable is used to test if a number is prime 1 = not prime and 0 = prime
        
        //this is required as the for loop is not entered when m = 1 and it is not a prime number
        //for loop not entered when m = 2 but it is a prime number
        if (m == 1)
        {
              test = 1;
        }
        
        //counts down from m-1 until it reaches 2
        for (int count = m-1; count > 1; count--)
        {
            //if m divided by count has no remainder then it is not a prime number     
            if (m % count == 0)
            {
                  test = 1;//this is the test condition for a prime number
                  break;
            }
        }
        //this outputs the prime numbers, i.e. when test == 0
        if (test == 0)
        {
           cout << m << " is a prime number." << endl;
        }
    }
    _getch();
    return 0;
}
Topic archived. No new replies allowed.