473,471 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Prime in C++

3 New Member
Hi Im a beginner in C++. I tried to do these but couldnt.
Pls hekp me to do these. thanks.

1. Design and implement a class called PrimeChecker. An instance of this
class will be able to check if a number between 2-10000 is prime. The class
stores the consecutive primes 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 in an array and uses
these to check if the given number is prime.
The class has the following public methods:
(a) Constructor
(b) Destructor
(c) IsPrime() which takes a parameter which is an integer in the range
1-10000 and returns a boolean.

2. Extend the class above so that if the number x (which may now be more
than 10000) to check is greater than the square of the largest prime in
the list of consecutive primes then the class adds new consecutive primes
to its internal array (the largest prime it adds is just greater than the
square-root of x.). Assume that you will never need to hold more
than 1000 consecutive primes.
Oct 28 '06 #1
3 3198
AricC
1,892 Recognized Expert Top Contributor
Use modulus


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int num;
  8.     cin >> num;
  9.     // num % 2 computes the remainder when num is divided by 2
  10.     if ( num % 2 == 0 )
  11.     {
  12.         cout << num << " is even ";
  13.     }
  14.  
  15.     return 0;
  16.  
Oct 30 '06 #2
hirunisha
3 New Member
HI, thanks for the reply.
But this won't sort my problem, its relate to classes.
Nov 1 '06 #3
hirunisha
3 New Member
I could write a porg to check whether a number is prime or not.

#include<iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cout<<" Enter a number to find if its a prime number "<<endl;
cin>>n;

bool a =true;

for(int i=2;i<sqrt(n);i++)
{

if(n%i==0)
{
a=false;
break;
}
}


if(a==false)
cout<<n<<" is not a prime number"<<endl;
else
cout<<n<<" is a prime number"<<endl;

}


I dnt knw hw to develope this for the followings:


1. Design and implement a class called PrimeChecker. An instance of this
class will be able to check if a number between 2-10000 is prime. The class
stores the consecutive primes 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 in an array and uses
these to check if the given number is prime.
The class has the following public methods:
(a) Constructor
(b) Destructor
(c) IsPrime() which takes a parameter which is an integer in the range
1-10000 and returns a boolean.

2. Extend the class above so that if the number x (which may now be more
than 10000) to check is greater than the square of the largest prime in
the list of consecutive primes then the class adds new consecutive primes
to its internal array (the largest prime it adds is just greater than the
square-root of x.). Assume that you will never need to hold more
than 1000 consecutive primes.
Nov 1 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
0
by: ETM11871 | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
2
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { ...
10
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.