#include <iostream>
usingnamespace std;
int main(){
int one;
int two;
cout << "Please enter two different numbers and I will tell you what numbers between are not prime numbers." << endl << endl;
cout << "Enter your first number" << endl;
cin >> one;
cout << "Enter your second number" << endl;;
cin >> two;
while(one <= two){
if(one%2 == 0){
cout << one << " is not a prime number." << endl;
}
elseif((one%3 == 0)&&(one!=3)){
cout << one << " is not a prime number." << endl;
}
elseif((one%5 == 0)&&(one!=5)){
cout << one << " is not a prime number." << endl;
}
elseif((one%7 == 0)&&(one!=7)){
cout << one << " is not a prime number." << endl;
}
one++;
}
system("pause");
}
It's very simple.
replace lines 13 and line 14 with the following code:
1 2
one++; // [line 13] (before while loop condition) You start by checking the number after the 'one'
while(one <= two-1) // [line 14] and you check until the number before 'two'
I just wanted to mention about your algorithm, it is only going to work for numbers up to 49 (72), which is rather limited, and your code is not scalable. If one has a list of all the numbers, then removes multiples of all the numbers up to N, then the numbers up to N2 will be prime.
So rather than repeating your code for each number, try to think of a way of doing it using loops.
On the wiki page for prime numbers, at the end there is the Euler's method. Have a go at implementing that.
First you need to figure out which of the 2 numbers entered is the smallest.
Then start a loop for the smallest integer till the highest integer.
Second you need to remember what the conditions for a prime is.
A prime number is a number that has only two factors: 1 and itself.
You need to setup a for loop that checks for factors for each number in the range entered.
Also you should check if the numbers are the same and only display prime for that number.
#include <iostream>
usingnamespace std;
int main(){
int one;
int two;
int small;
int large;
int factorcount;
cout << "Please enter two different numbers and I will tell you what numbers between are not prime numbers." << endl << endl;
cout << "Enter your first number" << endl;
cin >> one;
cout << "Enter your second number" << endl;;
cin >> two;
while(cin)
{
if (one == two)
{
if (one == 1 || one == 2 || one <= 0) // Integers must be:postive,non-zero,not "1" or "2."
cout << number << " is not a prime." << endl << endl;
else
{
for (int i=1; i<=one; i++) // Determines factors
if (one% i == 0)
factorcount++;
if ( factorcount == 2) // If factor are 2 then display prime
cout << number << " is a prime." << endl << endl;
else
cout << number << " is not a prime." << endl << endl;
factorcount = 0;
}
}
if( one < two)
{
one = small;
two = large;
}
else
{
two = small;
one = large;
} // this if, else loop will set the smallest integer as "small" and the larger integer as "large"
for (int i=small; i < large; i++)
{
if (i== 1 || i== 2 || i<= 0) // Integers must be:postive,non-zero,not "1" or "2."
cout << i<< " is not a prime." << endl << endl;
else
{
for (int j=1; j<=i; j++) // Determines factors
if (i% j == 0)
factorcount++;
if ( factorcount == 2) // If factor are 2 then display prime
cout << number << " is a prime." << endl << endl;
else
cout << number << " is not a prime." << endl << endl;
factorcount = 0;
}
}
system("pause");
}
I didn't check brackets or anything; not sure if this will even compile.