# Display Prime Numbers with a Number Class

Page 1 of 1

## 7 Replies - 372 Views - Last Post: 07 November 2017 - 08:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407437&amp;s=59a473bdf2675cefb26bf03d5ecb2228&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 KING iMm0rt4lxM

• New D.I.C Head

Reputation: 1
• Posts: 44
• Joined: 08-November 15

# Display Prime Numbers with a Number Class

Posted 06 November 2017 - 08:22 PM

I have to display the prime numbers between 2 and the number entered with a Number class. Compiler says I need an argument on display_Prime_Numbers. Therefore program is not running.

Any help would be appreciated it.

``` // Number.h
#ifndef NUMBER_H
#define NUMBER_H

#include <iostream>
using namespace std;

class Number
{
public:
Number();
//Number(int);
//bool isPrime();
//bool isDivisibleBy(int);
bool display_Prime_Numbers(int);
int get_Number();
private:
//int value;
int number_entered;
};

#endif // NUMBER_H

```

``` // Number2.cpp
#include <iostream>
#include "Number.h"

using namespace std;

Number::Number()
{
// Constructor
}

bool Number::display_Prime_Numbers(int i)
{
bool isPrime = true;

for (int i = 2; i < get_Number(); i++)
{
for (int j = 2; j * j <= i; j++)
{
// Checks if number is even
if (i % j == 0)
{
isPrime = false;
break;
}
}
}

// *** OUTPUT ***
if (isPrime)
{
cout << i << " ";
}

return isPrime;
}

int Number::get_Number()
{
cout << "Enter a positive integer number greater than 1: \n";
cin >> number_entered;

return number_entered;
}

```

```#include <iostream>
#include "Number.h"
#include "Number2.cpp"

using namespace std;

int main()
{
Number output;

output.get_Number();

output.display_Prime_Numbers();

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Display Prime Numbers with a Number Class

### #2 snoopy11

• Engineering ● Software

Reputation: 1437
• Posts: 4,626
• Joined: 20-March 10

## Re: Display Prime Numbers with a Number Class

Posted 06 November 2017 - 08:31 PM

You have made

```display_Prime_Numbers()
```

take an int as an argument ie

```display_Prime_Numbers(int i)
```

I would remove the reference to int i .....
Was This Post Helpful? 0

### #3 KING iMm0rt4lxM

• New D.I.C Head

Reputation: 1
• Posts: 44
• Joined: 08-November 15

## Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 12:58 AM

I managed to make the code run, but I don't understand why the number variable is not been included onto the display_Prime_Numbers function. Any ideas?

My output is basically "2 3 0", doesn't matter what number I write in.

```#include <iostream>
#include "Number.h"

using namespace std;

Number::Number()
{
// Constructor
}

bool Number::display_Prime_Numbers()
{
bool isPrime = true;

for (int i = 2; i < number; i++)
{
for (int j = 2; j * j <= i; j++)
{
// Checks if number is even
if (i % j == 0)
{
isPrime = false;
break;
}
}

// *** OUTPUT ***
if (isPrime)
{
cout << i << " ";
}
}
return isPrime;
}

int Number::set_Number()
{
cout << "Enter a positive integer number greater than 1: \n";
cin >> number_entered;

return number_entered;
}

void Number::get_Number()
{
number = number_entered;
}

```

```#include <iostream>
#include "Number.h"
#include "Number2.cpp"

using namespace std;

int main()
{
Number output;

output.set_Number();
output.get_Number();

cout << output.display_Prime_Numbers();

return 0;
}
```

```#ifndef NUMBER_H
#define NUMBER_H

#include <iostream>
using namespace std;

class Number
{
public:
Number();
//Number(int);
//bool isPrime();
//bool isDivisibleBy(int);
bool display_Prime_Numbers();
int set_Number();
void get_Number();
private:
//int value;
int number;
int number_entered;
};

#endif // NUMBER_H
```

Was This Post Helpful? 0

### #4 ndc85430

• I think you'll find it's "Dr"

Reputation: 702
• Posts: 2,871
• Joined: 13-June 14

## Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 01:12 AM

When do you ever initialise the member variable number? Hint: what does your constructor do?
Was This Post Helpful? 1

### #5 snoopy11

• Engineering ● Software

Reputation: 1437
• Posts: 4,626
• Joined: 20-March 10

## Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 01:20 AM

Wilson's general theorem for prime numbers is given as

for all n.

taken from

https://en.wikipedia...mula_for_primes

your theorem is not valid.

A prime is a natural positive number that is only divisible by 1 and itself.

taken from here

https://en.wikipedia...ki/Prime_number

have a think on your method.
Was This Post Helpful? 0

### #6 GazinAtCode

• D.I.C Head

Reputation: 33
• Posts: 121
• Joined: 26-September 16

## Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 01:53 AM

Your isPrime flag seems to be initialized in the wrong scope. Try to fix that. Also, your code can be improved/simplified. I would even venture to say that using a class in your example is a bit of an overkill (unless you're required to do so).

By the way, the 0 is the boolean output of the display_Prime_Numbers member function. I'm not sure you even want that.

(I'm referring to the second version of your code.)

This post has been edited by GazinAtCode: 07 November 2017 - 02:26 AM

Was This Post Helpful? 0

### #7 CTphpnwb

• D.I.C Lover

Reputation: 3732
• Posts: 13,569
• Joined: 08-August 08

## Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 05:39 AM

KING iMm0rt4lxM, on 06 November 2017 - 10:22 PM, said:

Compiler says I need an argument on display_Prime_Numbers.

Read up on scopes.
Was This Post Helpful? 0

### #8 KING iMm0rt4lxM

• New D.I.C Head

Reputation: 1
• Posts: 44
• Joined: 08-November 15

## Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 08:34 AM

Thank you! It seems my prime function was not correct and also my constructor was not initialized. After correcting those 2 things the program ran smooth.

And yes, I'm require to use a class for this particular assignment
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }