hi every body
this is my first post in this wonderfull web site
i'm a biggener in c++ and i have a q

i want to do a function that determines whether a number is prime and then use this function in a program that prints all the prime numbers between 1 and 10000

this is what i done and the result is wrong ... i don't know why

using namespace std;
int z;
bool prime(int x)
	for(int i=2;i<x,i>x;i++)
			return false;
	while (z)
		return true;
int main()

	int k=0;
	for(int i=0;i<=10000/2;i++)
		bool y=prime(i);
			cout<<i<<" is prime"<<endl;
			cout<<i<<" is not prime "<<endl;
		return 0;

what do u think ?

a integer is said to be prime if it is divisible by only 1 and itself
for example 2,3,5 and 7 are prime but 4,6,8 and 9 are not

for(int i=2;i<x,i>x;i++)

doesnt make sense to the compiler.
i guess you should use the (||) meaning "OR".

for(int i=2;i<x,i>x;i++) is wrong! Use for(int i=2;i<x;i++)

while (z)
return true;

is wrong! When you come to this point in your code you know you have a prime so why not just use return true;
Btw. in your for loop you don't have to test all the way to x. It is sufficient to test just to the square of x. But that may be a bit too high tech for the moment. Try to figure it out if you can, why that is so.

>>Btw. in your for loop you don't have to test all the way to x. It is sufficient to test just to the square of x.
He meant the square-root and not the square of x. ( must be a typo)

To the OP,
The suggestion of ddanbe is the most valuable here. Read it thoroughly.
As you told ( and we fully agree) you are beginner in C++. Also, the problem which you are doing is very popular and standard. I would suggest you to look a good code that determines if a number is prime or not and learn from it.
This advice would not be given to one who had not tried. As you did, and are nearly on the right track, you should perhaps study someones code.

Regarding the main() function, why are printing even those number which are not prime? You should check if a number is prime or not ( by using the function you wrote) and print it only if it is prime, else don't

Nice posting!

Thanks siddhant3s for pointing that out! Because english is not my native language I sometimes get the two mixed up. It has to be square root of course!

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

//skips even numbers and only checks squared values
//reduces the loop size runtime

int IsPrime(long number);

int main(int argc, char *argv[]){
	long n;
   printf("Enter number to check for primality:");
	if(IsPrime(n)) {
   	printf("Number is a prime\n");
   } else {
      printf("Number is not a prime\n");

   int c;
   printf("Press any key to continue..");
   c = getch();

int IsPrime(long number) {
    long i;

    i =2;

    if (number == 2) {
       return 1;

    if (number % i == 0) return 0;
    for (i=3; (i*i)<=number; i+=2) {
        if (number % i == 0) return 0;
    return 1;

Edited 6 Years Ago by technology: n/a

This article has been dead for over six months. Start a new discussion instead.