C++ prime number

Here's a step-by-step explanation of a C++ program to check if a number is prime:

Step 1: Include the necessary header files:

#include <iostream>
#include <cmath>

Step 2: Define the main function:

int main() {
    int num, i;
    bool isPrime = true;

    std::cout << "Enter a positive integer: ";
    std::cin >> num;

    if (num <= 1) {
        isPrime = false;
    } else {
        for (i = 2; i <= sqrt(num); i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }
    }

    if (isPrime) {
        std::cout << num << " is a prime number.";
    } else {
        std::cout << num << " is not a prime number.";
    }

    return 0;
}

Step 3: Define variables for the input number and iteration, and a boolean flag for prime status.

Step 4: Prompt the user to enter a positive integer.

Step 5: Check if the number is less than or equal to 1. If so, set the prime flag to false.

Step 6: Use a for loop to iterate from 2 to the square root of the input number. Check if the number is divisible by the iteration variable. If it is, set the prime flag to false and break the loop.

Step 7: Output whether the number is prime or not based on the prime flag.

Step 8: Return 0 to indicate successful completion of the program.