+ 4

How to write a program can know if the number prime or not by c++

Question

29th Apr 2019, 3:22 PM
Mohamed
Mohamed - avatar
3 odpowiedzi
+ 7
The logic is very simple. First of all ask yourself what makes realy a prime number or what is the condition for a prime number, so the answer will be if it is divisible by 1 and itself only. For example, 3 is a prime number as it is divisible by 1 and itself only. No other number can completely divide it(remainder = 0 when divide). So, the logic will be quite simple. Start with a bit conditions that can be applied on numbers regarding its prime number criteria. Apply a loop whose value will start from 2 and go to x-1(here x is the main number entered by the user). If the number is divisible by any of the numbers in this loop, then it is not a prime number else if the loop ends and no number divides it completely then it simply means that it is a prime number. Please note that : negative numbers should not be included and 1 is neither a prime number or a composite number. Hope it helps:)
30th Apr 2019, 2:21 PM
Ayush Sinha
Ayush Sinha - avatar
+ 6
https://code.sololearn.com/c2s7rm0jni1z/?ref=app https://code.sololearn.com/cI2wnAbW05C4/?ref=app
30th Apr 2019, 2:13 PM
Ayush Sinha
Ayush Sinha - avatar