A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is only divisible by 1 and itself.
C++ program to check whether a given number is a prime number or not:
C++ Code:
#include <iostream>
using namespace std;
bool isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i * i <= number; ++i) {
if (number % i == 0) {
return false;
}
}
return true;
}
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (isPrime(number)) {
cout << number << " is a prime number." << endl;
} else {
cout << number << " is not a prime number." << endl;
}
return 0;
}
This program defines a function `isPrime()` that takes an integer as input and returns true if the number is prime and false otherwise. Then, in the `main()` function, it prompts the user to enter a number, checks if it's prime using the `isPrime()` function, and outputs the result accordingly.