Prime Numbers

An integer greater than 1 is a prime number if it does not have a divisor that is greater than 1 and less than itself.

For examples, 2,3,5,7,11,13,17 are all the primer numbers, and 10 is not a primer number (because 10 have a divisor 2 and a divisor 5).

Write a program function “bool isPrime( int x)” to determine whether an input number x is a prime number or not. The function isPrime takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate your function is correct.


Help.

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
48
49
50
51
52
 #include <iostream>
using namespace std;

// Function declaration
bool isPrime(int number);
int main()
{
// Declaring variables
int num;

/* This while loop continues to execute
*until the user enters a valid number
*/
while (true)
{
// Getting the input entered by the user
cout << "Enter a number (>1) :";
cin >> num;

if (num <= 1)
{
cout << "** Invalid.Must be greater than 1 **" << endl;
continue;
}
else
break;
}

// Based on the function return value display appropriate message
if (isPrime(num))
cout << num << " is a Prime Number." << endl;
else
cout << num << " is not a Prime Number." << endl;

return 0;
}

/* This function will check whether the number is
* prime or not
*/
bool isPrime(int number)
{
// If the user entered number is '2' return true
if (number == 1)
return false;
for (int i = 2; i * i <= number; i++)
{
if (number % i == 0)
return false;
}
return true;
}
Help.


Not psychic. Can't see your monitor. What does it not do that you want it to do, or what does it do that you don't want it to?
You should indent your code to reflect the actual structure. Otherwise it's nearly impossible to see the structure and you're likely to mess it up.

The program works for me. What problem are you having?

The isPrime() check can be made more efficient in many ways. One of the easiest is to check for divisible by 2 first, and then only check odd numbers after that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool
isPrime(int number)
{
    // If the user entered number is '2' return true
    if (number == 1)
        return false;
    if (number % 2 == 0) {
      return false;
    }
    for (int i = 3; i * i <= number; i += 2) {
        if (number % i == 0)
            return false;
    }
    return true;
}

If it works for everyone else but you're asking for help, I'm going to guess its this problem:

In your compilers options, tell it that the terminal window shouldn't close immediately when your program exits.

Alternately, open either an xterm, a cmd prompt, or whatever your OS uses and then us the cd (change directory) command to get to wherever your program executable is being stored when you compile... type it's name or perhaps ./ then its name under unix/linux, then it will run but return you to the command prompt afterwards without erasing everything.
Topic archived. No new replies allowed.