Hey, I've got an assignment from my computer teacher in school.
I've got to make a program that displays the prime numbers in a range and also display the number of prime numbers that are present.
And i'm only supposed to use the basic header files.....that means the program should start of something like this -:

#include<iostream.h>
#include<conio.h>
#include<math.h> \\if needed
int main()
{


so can anyone please help me do this..............thank you!

1) First create a prime number generator
2) The adjust it accodignly to what you need it.

A prime number is one where its 2>= | n | < MAX, n != even, n is evenly divisible its self and 1 only.

A few primes are 2,3,5,7,11.

5 is a prime because its divisible only by 1 and its self evenly.
5/5 = 1 with 0 remainder
5/1 = 5 with 0 remainder

5 mod 4 != 0
5 mod 3 != 0
5 mod 2 != 0


Some hints. Create a isPrime(int num), function.

bool isPrime(int num)
{
   //if num is equal to 2 then return true;

   //check if num is divisible by anything else other than its self
    for( i = 2; i < num; i++) 
    {
        // if num mod i is equal to 0 then num is not a prime number so return false
     }

//if it passed the loop then it is a prime number so return true
}

Now inside your main use a for loop to check is a number is prime

//inside your main
 for i is equal to 2, until MAX 
  {
          //call isPrime with the argument being i. check if it return true 
          // if it return true then print to screen that it is a prime
          //else do nothing
   }

After you get this working, then change it to what you need it to be.

It's <iostream> not <iostream.h>, avoid <conio.h>, and you probably won't need <cmath>.

Now let me freak your mind: You're using Turbo C++, or tutorials for it? Right?

There's a simple bit scanning method you can throw together with the links I posted on another thread: http://www.daniweb.com/forums/thread204950.html

well the thing is that .....i use an older version of C++ ie. Borland C++ 4.5
so i need the program based on that format .......right now i've got a program that kinda looks like this-:

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
    int prime=0,r1,r2,n=0;
    cout<<"\n Enter range \t";
    cin>>r1>>r2;
    for(int i=r1;i<=r2;i++)
    {
        prime++;

        for(int j=0;j<=sqrt(i);j++);
        {
          prime=i;
          if(i%2==0)
          {
            n++;
          }
          else if
          {
            cout<<prime;
          }
        }
     }
     cout<<"\n Number of primes \t";
     cout<<n;
     getch();
     return 0;
}

but the necessary out put does not come.........so please help me out.......or my crazy teacher at skool is gonna screw me .....

commented: no code-tags -3

but the necessary out put does not come.........

Of course the necessary output doesn't come. You didn't expect it to, did you? You have no algorithm to test for/calculate prime numbers. There are a lot of ways to do this. firstPerson listed one way. If you don't have any other algorithm that you want to use, implement his. He's posted a skeleton that you can work off of.

bool isPrime(int num)
{
   if(num < 2 ) return false;
   else if(num == 2) return true;

     for(int i = 2; i < ceil ( sqrt(num) ); i++)
             if(num % i == 0) return false;
  return true;
}

void printPrime(const int Max = 100)
{
     for(int i = 2; i  < MAX; i++)
            if( isPrime(i)) cout << i <<" is Prime\n";

}
bool isPrime(int num)
{
   if(num < 2 ) return false;
   else if(num == 2) return true;

     for(int i = 2; i < ceil ( sqrt(num) ); i++)
             if(num % i == 0) return false;
  return true;
}

void printPrime(const int Max = 100)
{
     for(int i = 2; i  < MAX; i++)
            if( isPrime(i)) cout << i <<" is Prime\n";

}

When I test your code , it did not work correctly everytime
I also do not find what is the problem , I'm confused in this line
for(int i = 2; i < ceil ( sqrt(num) ); i++)
because if n=9 then it's prime

When I test your code , it did not work correctly everytime
I also do not find what is the problem , I'm confused in this line
for(int i = 2; i < ceil ( sqrt(num) ); i++)
because if n=9 then it's prime

Try changing the < in line 6 to <=.

Hey Mafia, I got about the same task some time ago, and I wrote my code using a bitwise buffer to store all uneven numbers from 3 till the entered number, and then by using 'The Sieve of Eratosthenes' to sort out the numbers, simply picking the first one, and removing all that divides with this from the buffer. Then picking the next number like and dividing that thoughout the buffer.

The buffer will be like this;
3 5 7 9 11 13 15 ...

The program now finds 3, writes that to the screen, then does the following, by dividing out the buffer;
5 7 11 13 15 ...

Then it findes 5, and does the same:
7 11 13 ...

And so on, atm I'm trying mulithread the application using OpenMP (Started progamming this summer, so I'm not really good at multitheading applications yet), but as single threaded it's able to find all the primenumbers in the domain of 2-100.000.000 (5761454Primes) in just under 12seconds :), so it's quite effective. ^^that's using about 6mb of ram, and 2GHz, on my Laptop :), so I'm quite satisfied about the code. There are quicker ways, but my professor was impressed, as he was expecting a simple primetest algorithm, like take a number, use it in a modulo loop, to check if any number above to divided with it returns 0. This will work ofc. however it's VERY slow, and ineffecient, so use 'The Sieve of Eratosthenes' method, if you're able to. The bitwise buffer, is simply to use less memory :) - and to speed up things a bit :).

EDIT: The program only found 5761453, as I include 2 myself ofc :D
Btw; Code is quite messy and comments are written in danish, so wont really post it here, I'm sorry :o. Would simply be too messy to understand.

EDIT2: Here's a sample of my first sketch of a simple test algoritm, which does the job, but slowly:

//the calulation function.
void calc (unsigned int number, unsigned int i) //using "void" instead of "int", because the funktion doesn't need to return a value.
    {
        //define variables
    bool Primecheck; //bool, that indicates wether the number is a prime or not (1 = Prime, 0 = Not a prime). Bool is used instead of int, since it only has to store 2 values (1 and 0).
    unsigned int a = 0; //set a = 1, used to number the primes.
    unsigned int j;

        //write welcome message
    cout << endl << "The program is finding primes in the domain of \"" << i << " ---> " << number << "\"" << endl; //tell the entered domain on the screen
    out << "The program is finding primes in the domain of \"" << i << " ---> " << number << "\"" << endl; //same as above, just to the file.
    
        //Calcul funktion
    for (; i<=number; i++) //run the "i" loop, when "i" is less or equal (<=) to the input number., then +1 for each completed cycle.
        {
        Primecheck=1; //set the boolic value of primecheck = true, for the "i" loop. (to avoid a negativ loop, basically you'll have to set "not prime" each cycle, because everything is considered a prime at this point.)
        
            //Filtering function, filters all non primes, and sets "primecheck = 0", if the tested number isn't a prime.
        for (j=2; ((j < i) && (Primecheck==1)); j++) //run the "j" loop, run this until the "j" value is equal to the "i" value, by plussing 1, each time the cycle completes, starting from 2.
            {if ((i%j)==0){Primecheck=0;}} //if "i" divided by "j" equals "0 in remainder", then set "Primecheck = 0" (number is not a prime). If i%j=0, then that's an devident, and therefore "i" isn't a prime number.

            //Screen / file output
        if(Primecheck!=0) //if number is a prime, then write output.
            {
            a++; //add +1 to "a", simply to be ready number the next prime.
            cout << a << ". " << "Prime:\t"<< i << endl;  //screen output - write "a" (1 as default (used to show which number in the prime number chain the prime number is)), then write "Prime:" then the number (the prime) "i".
            }

        //terminate for "i" loop, when "i" has reached the max domain number.
        }

        //write end message
    cout << a << " primes was found; in the given domain!" << endl << endl; //announce the number of primes found on the screen.

    //terminate function
    }

^^Bad English, over commenting, and stuff, I wrote this code like a week after I learn'd that C++ even existed, so I know it sucks :D

// Skeen

main()
{
int i,j=2,ch=0;
clrscr();
printf(

main()
{
int i,j=2,ch=0;
clrscr();
printf(

What on Earth does this post mean and what does it have to do with prime numbers?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.