I have 3 instructions-
1- Use rand() to generate a number less then 100 and greater than 1
2- Call the prime() function to test the result
3- Print out the result

So far i have found how to get a prime number but don't know how to fill up the conditions.(how to use rand() function)

#include <iostream>
using namespace std;

int smallFactor(int x) {
   int answer = 2;
   while (x % answer != 0) answer++;
   return answer;
}

int main() {
   int n;
   cout << "Enter a number greater than 1: " << endl;
   cin >> n;
   if (n<1 ||n>99) cout<<"Illegal";
   else if (n == smallFactor(n)) cout << "That is prime.";
   else cout << "That is not prime.";
   cout << endl;
   return 0;
}

You say rand() and it returns a random number between 0 and RAND_MAX.

That's how you use it.

You can call srand(something) to initialize the random number generator -- where something is a relatively unpredictable value, such as the current time.

>>while (x % answer != 0) answer++;
1) The code may be short but gives more work to the computer. You have to check if x is divisible by a number lesser than its square root and thats all you have to do. So In this case the while loop need not execute more than 10 times at the maximum.
2) What will happen if the input is 1?

i need it to create a random number and to print out whether this random number is prime or not!! On tutorial there is noting like that :(

If you are having problem with rand() go google 'rand cpp reference'. It has a nice example as to how to use it.

And like the above poster has mentioned,

>>while (x % answer != 0) answer++;

is not a good practice. Since you will check 2, then 3, then 4 then 5 then 6 and so forth until you reach the number. And once you know it is not divisible by 2, it's also not divisible by 4 or 6 or any multiples of 2 (which comes out to be even numbers).

A tutorial isn't generally going to tell you how to make the program you want to make :P . Here's a simple prime function

bPrime = true;
for (int ii(2); ii < _iVal / 2; ii++)
{
    if (!(_iVal % ii))
        {
             bPrime = false;	
             break;
        }
}

Where iVal is the number you are checking against. Here is a random number function:

int iRand = rand() % MAX;

Note it's unseeded. If i remember correctly to seed it you just use srand(INT_VALUE) . Usually this is done with the time (ie clock()) to make a quite random pseudo-random number. I'm gonna let you put the 2 ideas together yourself, so you at least learn something.

Another solution
int Prime(int k)
{
int x;
for(int i=1;i<k;i++)
if(k%i==0) x++;
return x;
}

in main method u have to write
int 9;
if(prime(9)>1)
cout<<"9 is prime ";
else cout<<"not prime";

Another solution
int Prime(int k)
{
int x;
for(int i=1;i<k;i++)
if(k%i==0) x++;
return x;
}

in main method u have to write
int 9;
if(prime(9)>1)
cout<<"9 is prime ";
else cout<<"not prime";

That's an ok solution, but can be optimized for efficiency in 2 ways - you need only mod up to (k / 2) in your loop. Any number larger than half of k can't be multiplied by any integer to equal k, so the last k / 2 calculations will always result in a remainder. Also, it should break out as soon as the number is found to be not prime, otherwise your just wasting clock cycles calculating what you already know.

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.