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.
#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
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.
#include <iostream>
#include <conio.h>
usingnamespace std;
int main ()
{
unsignedint 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;
}