From e9223428d3a8fadd2c522a446bdbd18d9c24fcf1 Mon Sep 17 00:00:00 2001 From: suddhasattwakhan <72450994+suddhasattwakhan@users.noreply.github.com> Date: Fri, 1 Oct 2021 17:30:57 +0530 Subject: [PATCH] Added a program to determine prime number --- CPP Programs/Primeno.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 CPP Programs/Primeno.cpp diff --git a/CPP Programs/Primeno.cpp b/CPP Programs/Primeno.cpp new file mode 100644 index 0000000..1cdb014 --- /dev/null +++ b/CPP Programs/Primeno.cpp @@ -0,0 +1,30 @@ +//Prime Number +#include +using namespace std; + +int main() { + int i, n; + bool isPrime = true; + + cout << "Enter a positive integer: "; + cin >> n; + + // 0 and 1 are not prime numbers + if (n == 0 || n == 1) { + isPrime = false; + } + else { + for (i = 2; i <= n / 2; ++i) { + if (n % i == 0) { + isPrime = false; + break; + } + } + } + if (isPrime) + cout << n << " is a prime number"; + else + cout << n << " is not a prime number"; + + return 0; +}