#include <iostream>
usingnamespace std;
int main() {
cout << 2 <<endl;
for (int i=3; i <= 19; i+=2)
{
if(((i % 2)!=0) && ((i % 3)!=0) && ((i % 5)!=0) && ((i % 7)!=0)) // from 1 to 20 that's enough
cout << i <<endl; // but there are more fancier methods
// of computing prime numbers, try to search.
}
return 0;
}
#include <iostream>
usingnamespace std;
//using namespace system;
int isPrime(long num) // Returns 1 (true) if its prime, or 0 (false) if not
{
if (num < 2) // 1 is not a prime number
return 0;
// if it is > 2 and an even number, then it definitely is not a prime
if (num > 2 && (num % 2) == 0)
return 0;
//considering the fact all can be divided by 1 and itself,
//start checking if there is any other divisor, if found one then no need to continue, it is not a prime
for(int i = 2; i < num; i++ )
{
cout << " divisor: " << i << endl;
if ( (num % i) == 0) // if it is divisible by i
{
// a divisor other than 1 and the number itself
return 0; // no need for further checking
}
}
return 1; // if all hurdles/checks are crossed, heyyyy, its a prime
}
int main()
{
int num;
do {
cout << " enter a number (0 to stop) " << endl;
cin >> num;
if (num) {
if (isPrime(num))
cout << num << " is a prime numebr " << endl;
else
cout << num << " is NOT a prime numebr " << endl;
}
} while (num > 0);
return 0;
}
And I'm sorry Imust have mis read this the first time, I though you wanted to print odd numbers my bad... Here is the correct and dynamic method to use for this...
yeah it depends on what you consider like "dynamic" what I meant by dynamic is that what ever number you plug into it, it willl return all of the prime numbers up to that number...
I have a similar problem with my program but i need it to list the divisor its sapose to look like this but for some reason it wont compile :|
Input a positive integer : 8
Testing 1 ->
The divisors: 1
The number 1 is not a prime number.
Testing 2 ->
The divisors: 1 2
The number 2 is a prime number.
Testing 3 ->
The divisors: 1 3
The number 3 is a prime number.
Testing 4 ->
The divisors: 1 2 4
The number 4 is not a prime number.
Testing 5 ->
The divisors: 1 5
The number 5 is a prime number.
Testing 6 ->
The divisors: 1 2 3 6
The number 6 is not a prime number.
Testing 7 ->
The divisors: 1 7
The number 7 is a prime number.
Testing 8 ->
The divisors: 1 2 4 8
The number 8 is not a prime number.