write the program that tells you whether the number you enter is a prime number. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

write the program that tells you whether the number you enter is a prime number.

9th Apr 2018, 8:22 PM
Mohamed Atito
Mohamed Atito - avatar
3 Answers
+ 1
int num = 3; if(num > 2){ for(int i=0;i < num/2;i++){ if(num%i == 0){ return true; } } } return false;
9th Apr 2018, 8:44 PM
Ariela
Ariela - avatar
+ 1
template <typename Int> bool prime(const Int num) { if (num <= 3) return true; for (Int i{2}; i != num / 2 + 1; ++i) if (num % i == 0) return false; return true; } #include <iostream> int main() { long long in; std::cin >> in; std::cout << std::boolalpha << prime(in); }
10th Apr 2018, 4:16 PM
Timon Paßlick
+ 1
Ariela Your solution doesn't compile and every result is wrong. 1 -> false 2 -> false 3+ -> 0 modulus -> undefined, maybe running forever Not meaning to blame you, I'm just honest.
10th Apr 2018, 4:18 PM
Timon Paßlick