-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Rohit Jindamwar <rohit@xperate.com>
- Loading branch information
1 parent
a3cde14
commit 4772df7
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |