Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,562 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,914 people online right now. Registration is fast and FREE... Join Now!




prime number

 
Reply to this topicStart new topic

prime number

anis_ahmad
18 May, 2008 - 08:26 AM
Post #1

New D.I.C Head
*

Joined: 4 May, 2008
Posts: 7

I am trying to check the given number is prime or not
exactly i could not get logic.
prime number is divisible by 1 or number itself. but i am unable to find the logic to chek number is prime or not
User is offlineProfile CardPM
+Quote Post

homemade-jam
RE: Prime Number
18 May, 2008 - 08:55 AM
Post #2

eeeAddict
Group Icon

Joined: 17 Mar, 2008
Posts: 1,066



Thanked: 1 times
Dream Kudos: 25
My Contributions
Anis,

What you wanna do is have your code take the input from the user; square root it (as you will not have to divide by the numbers below the square root to prove whether it is prime) and then, beginning at 2 find the modulus (remainder) of your users input divided by all numbers upto the square root.

If this modulus is 0 then the number is not prime (it divides cleanly).

If the modulus is not prime then increment the divisor by one.

Continue the process until the divisor is equal to the square root - if none of the divisions gave a modulus of 0 then the number is prime.

Jam
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month