Skip to content

Commit

Permalink
Added code for isPrimeNumber (#430)
Browse files Browse the repository at this point in the history
Co-authored-by: Rohit Jindamwar <rohit@xperate.com>
  • Loading branch information
rohit85-it and Rohit Jindamwar authored Oct 7, 2023

Verified

This commit was signed with the committer’s verified signature.
ararslan Alex Arslan
1 parent a3cde14 commit 4772df7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cpp/isPrimeNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
using namespace std;

int main() {

int i, n;
bool is_prime = true;

cout << "Enter a positive integer: ";
cin >> n;

// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
is_prime = false;
}

// loop to check if n is prime
for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}

if (is_prime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";

return 0;
}

0 comments on commit 4772df7

Please sign in to comment.