Prime number between two values

I wrote this program to print prime numbers between 2 and 10 but it only prints '3'. how can I fix this??

here is the code

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

using namespace std;

int prime(int x, int y);

void main(){
	int n=2;
	int m=10;

	cout<<"prime numb :"<<prime(n,m);	

	_getch();
}

int prime(int x,int y){
	int c=0,r=0;
	int z=(x+1);

	for(z;z<y;z++){
		for(int a=1;a<=z;a++){
			if(z%a==0)
				c++;
			}
		if(c==2)
			r=z;

		}
	return r;

}
Last edited on
kid a function can return only one value hence when it is called from main it will show only the final value r takes in the function .But then your question would be that we should have got 7. The answer is :
1/ do not start 'a' with value 1, initialize with 2.I mean 1 divides everything.
2/Do not use a return type function in this case.
3/ try this code it will work:

void prime (int m,int n)
{
int c,z;

z=(m+1);
for(z;z<n;z++)
{
c=1;
for(int a=2;a<z;a++)
{if(z%a==0)
c=0;
}
if(c)
cout<<z<<"/n";
}
}

and in main
simply call prime as below
cout<<"the prime no. are /n";
prime(2,10);
Last edited on
kid

Is there really any need to patronize other posters?

Also, please use code tags when posting code:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
How can I modify this to calculate prime numbers between any given numbers? because when I gave '0' and '10' it printed '1' as a prime number.


void prime (int m,int n)
{
int c,z;

z=(m+1);
for(z;z<n;z++)
{
c=1;
for(int a=2;a<z;a++)
{if(z%a==0)
c=0;
}
if(c)
cout<<z<<"/n";
}
}
It gave 1 as a prime because int m arrived in the function as '0' and was incremented on the fifth line of the function there z = (m+1). You could fix this by only allowing positive non-zero integers to be passed to the function. Another way of fixing this would be to trial division by all integers, including 1, and if it has exactly two divisors, then returning it as a prime.

Once you have this working, then you can work on speeding up your method. You need only check up to the square root of a number to see if it is prime and other than 2, you only need to divide by odd numbers*.

*Faster methods do exist and are more complex.
Last edited on
To check if somthing is prime, start with 2. If the number can be devided in 2 (even once) it is not prime. Next, divide by every sequential odd number:

example:

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
//prototype
bool isprime(/* args */)

//assume 'x' = int that we want to tell is prime or not
switch((x % 2) == 0)
{
    case true:
    {
         //true, we can devide by two, so it can't be prime!
    }
    break;

    case false:
    {
         //false, try every other odd number
         for(unsigned long int y = 3; y < A_LIMIT; y += 2)
         {
              //I'll leave the logic to you
         }
    }
    break;

    default:
         break;
}


That's just pseudo code to get you thinking. Hope this helps.
----------------------------------------------------------------------------------

Also, mats, you absolutely do not want to include 1!!! You should not check every integer, and just because it has two devisors does not make it prime. Every number has 2 devisors (1 and itself), so by your definition every number is prime, which is not true.

A prime number is a number that has only two devisors (1 and itself). That is why you should not test 1, because you will always get a valid result from that operation.

----------------------------------------------------------------------------------

Half of all numbers are even. Since all even numbers can be devided by 2, you can use modulo to narrow your results significantly. Aftwerwards, if it is still "prime", you just check every odd number after 2.
Last edited on
Every number has 2 devisors (1 and itself), so by your definition every number is prime, which is not true.


1
2
3
4
5
6
7
8
9
Number Divisors
1            1 <-- also note that this lacks even two divisors in total.
2            2
3            2
4            3
5            2
6            4
7            2
8            4


And my original quote:
...and if it has exactly two divisors, then returning it as a prime.


Although I agree it's unnecessary to check division by 1, I think it's important to remember in the end that a prime does have exactly two divisors.
Topic archived. No new replies allowed.