-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeOrPrime.cpp
More file actions
34 lines (28 loc) · 956 Bytes
/
CompositeOrPrime.cpp
File metadata and controls
34 lines (28 loc) · 956 Bytes
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
32
33
34
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
bool flag = true; // Initialize the flag as true, assuming the number is prime
// Loop through numbers from 2 to n-1 to check for factors
for (int i = 2; i <= n-1; i++) {
if (n % i == 0) { // Check if 'i' is a factor of 'n'
flag = false; // If a factor is found, set the flag to false (number is not prime)
break; // No need to check further, exit the loop
}
}
// Special case: 1 is neither prime nor composite
if (n == 1) {
cout << n << " is neither prime nor composite.";
}
// If flag is true, the number is prime
else if (flag == true) {
cout << n << " is a prime number.";
}
// If flag is false, the number is composite
else {
cout << n << " is a composite number.";
}
return 0;
}