From 213aa584fa8cb16e2d86a97dee13831abb19149e Mon Sep 17 00:00:00 2001 From: kumarsyadav2 <77455527+kumarsyadav2@users.noreply.github.com> Date: Thu, 7 Oct 2021 00:08:41 +0530 Subject: [PATCH] chore(CPlusPlus): add program to find prime number (#500) Co-authored-by: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> Co-authored-by: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> --- algorithms/CPlusPlus/Maths/prime-number.cpp | 34 +++++++++++++++++++++ algorithms/CPlusPlus/README.md | 2 ++ 2 files changed, 36 insertions(+) create mode 100644 algorithms/CPlusPlus/Maths/prime-number.cpp diff --git a/algorithms/CPlusPlus/Maths/prime-number.cpp b/algorithms/CPlusPlus/Maths/prime-number.cpp new file mode 100644 index 00000000..c50e8604 --- /dev/null +++ b/algorithms/CPlusPlus/Maths/prime-number.cpp @@ -0,0 +1,34 @@ +// It is a prime number program in c language. +// it is used to check whether a given number is prime or not + +#include //header + +using namespace std; +int main() { //main function + int num, i, a = 0; //define variable + cin >> num; //taking input from user + + for (i = 2; i <= num / 2; i++) { //for loop started + if (num % i == 0) { //if condition + a++; + } + } + if (num < 2) { + a++; + } + if (a == 0) { + cout << "Prime"; // printing if the number is prime + } else { + cout << "Non-Prime"; // printing if the number is non-prime + } + return 0; // returning from main function +} + +// sample input || sample output +// 1 || Nom-Prime +// 2 || Prime +// 3 || Prime +// 4 || Non-Prime +// 5 || Prime + +// time complexity of this program is O(n). diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 635007c7..70cc84ee 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -130,6 +130,8 @@ 8. [Reverse digit of a number](Maths/reverse-digits.cpp) 9. [Missing number](Maths/missing-number.cpp) 10. [Factorial of a number](Maths/factorial.cpp) +11. [Prime-number](Maths/prime-number.cpp) + # Recursion