Find prime number

Hi,i need to write program to print prime number between 1-20,please give me outline :)
Hi.. to be able to do this you are going to have to do something like this I believe...

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main(){
    for(int i = 1; i <= 20; i++)
    {
        if(i % 2 != 0){
            std::cout<< "\n" << i << "\n";
        }
    }
}
Thanks.I will check it.
CodyShort!---it work but what about 2..is not prime number?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace 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;
}
Last edited on
@jankidudel
1
2
3
4
5
#include <iostream>

int main() {
    std::cout << "2\n3\n5\n7\n11\n13\n17\n19" << std::endl; // from 1 to 20 that's enough
}
Hah..hah.Thanks Guys I got this 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace 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;
}
@mazd, all that commenting not proper indentaton

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...

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
#include <iostream>
#include <math.h>

using namespace std;

bool prime(int);

int main()
{
    for(int number = 1; number <= 20; number++){
    bool isPrime = prime(number);
        if (isPrime)
        {
            cout<< endl << number << endl;
        }
    }
}

bool prime(int number)
{

    for(int i = 2; i <= sqrt(number); i++)
    {
        if (number % i == 0){
            return false;
        }
    }
    return true;
}
@CodyShort that is not dynamic.
The dynamic method uses only the primes that were already found to compute the modulus operation.
The dynamic method uses only the primes that were already found to compute the modulus operation.

Isn't that called Sieve of Eratosthenes?
Ah I was just about to suggest the Sieve of Eratosthenes!
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...
@CodyShort

I got this error msg :(

Error 1 error C2668: 'sqrt' : ambiguous call to overloaded function
odd... what compiler are you using?
Visual C++ 2008
It run in xwDev C++ but it say..
press any key to continue.. <----after press it print 1, next press 2, next press 3...up to 19

ref.QTN
between 1-20
Last edited on
Error 1 error C2668: 'sqrt' : ambiguous call to overloaded function
sqrt has overloads for floats and doubles. Since you're passing an int, VC++ doesn't know which function to call. Use sqrtf.
Thanks hamsterman
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.
Topic archived. No new replies allowed.