+ 6

How can I check a number if prime.

What will be the program in c which can help me to check a number if it is a prime number. And also it tell me that number which numbers divided returning Zero.

1st Sep 2018, 11:21 PM
Md Habibullah
Md Habibullah - avatar
11 odpowiedzi
+ 4
#In python a = int(input()) factors = [i for i in range(1,a+1) if a%i == 0] if len(factors)==2: print(a,'is prime.') else: print(a,'is not prime.')
2nd Sep 2018, 8:22 PM
Shuaib Nuruddin
Shuaib Nuruddin - avatar
2nd Sep 2018, 12:43 AM
NimWing Yuan
NimWing Yuan - avatar
+ 5
Use the Erathnostenes algorithm. Get input For numbers from 2 to sqrt(num)+1 (for improving efficiency) If the number is divisie by the number in the loop, return false Else, return true.
2nd Sep 2018, 12:26 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
https://code.sololearn.com/c6yz0DMw8euz/#cpp My implementation in C++. Hope this helps.
2nd Sep 2018, 12:07 AM
Steppenwolf
Steppenwolf - avatar
+ 2
Seive of Eratosthenes... Google it.
2nd Sep 2018, 6:02 AM
Junth Basnet
+ 2
2nd Sep 2018, 7:31 AM
Junth Basnet
+ 1
Dark Angel What you described doesn't sound like Eratosthenes' algorithm.
2nd Sep 2018, 2:51 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Clementine💝 The Sieve of Eratosthenes is not that great for testing one number. And IMHO, for someone's first primality testing code, something simple would be better.
2nd Sep 2018, 7:03 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
for c there is no return typr like boolean so use 1 or 0 in place 1 for true and 0 for false nd use simple algorithm
3rd Sep 2018, 12:40 PM
Aarav Raj
Aarav Raj - avatar
0
a%2!=0 then a is prime number except 2 ,2 is prime number though
17th Aug 2019, 12:43 AM
Gunaraj
Gunaraj - avatar