Skip to content

Commit

Permalink
Check Prime Number in cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit11102001 authored Oct 31, 2022
1 parent 57984ea commit 23fe906
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions primenumverornot.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 23fe906

Please sign in to comment.