-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprime_1.cpp
31 lines (27 loc) · 914 Bytes
/
prime_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
// Function to check if a number is prime
void isprime(int num) {
// Check if the number is less than or equal to 1
if (num <= 1) {
cout << "\nThe Number " << num << " is Not Prime\n";
return; // Exit the function
}
// Loop from 2 to half of the number
for (int i = 2; i <= (num / 2); i++) {
// If num is divisible by i, it's not prime
if (num % i == 0) {
cout << "\nThe Number " << num << " is Not Prime\n";
return; // Exit the function
}
}
// If the loop completes without finding divisors, the number is prime
cout << "\nThe Number " << num << " is Prime\n";
}
int main() {
int a;
cout << "\nEnter the Number: ";
cin >> a; // Read a number from the user
isprime(a); // Call the isprime function to check if it's prime
return 0; // Exit the program
}