forked from vishal8113/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vishal8113#171 from shrutiujjwal24/master
Create primenumber.cpp
- Loading branch information
Showing
1 changed file
with
26 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,26 @@ | ||
// A school method based C++ program to check if a | ||
// number is prime | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
bool isPrime(int n) | ||
{ | ||
// Corner case | ||
if (n <= 1) | ||
return false; | ||
|
||
// Check from 2 to n-1 | ||
for (int i = 2; i < n; i++) | ||
if (n % i == 0) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
// Driver Program to test above function | ||
int main() | ||
{ | ||
isPrime(11) ? cout << " true\n" : cout << " false\n"; | ||
isPrime(15) ? cout << " true\n" : cout << " false\n"; | ||
return 0; | ||
} |