42 Piscine/42 Piscine: Preparation Day 2/Prime Number Checker

# Prime Number Checker

This C program assesses the primality of a user-provided number. It initiates by soliciting user input, promptly classifying any number less than or equal to 1 as non-prime. For numbers exceeding 1, the program employs a loop to scrutinize factors by dividing the number by integers from 2 to its square root. Encountering a factor, it sets the "isPrime" flag to false and concludes the loop. Ultimately, the program communicates the primality of the input number by referring to the "isPrime" flag.

```
#include <stdio.h>
#include <stdbool.h>

int main() {
    int num;
    bool isPrime = true;
    printf("Enter a number: ");
    scanf("%d", &num);

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

    if (isPrime) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }

    return 0;
}

```

## Code Breakdown

### #include <stdio.h> / #include <stdbool.h>

* These lines include the standard input-output library (`stdio.h`) and the standard library for boolean values (`stdbool.h`). The latter is used to define the boolean data type and the `true` and `false` constants.

### int main() {

* This line is the beginning of the `main` function, which is the entry point of a C program. It returns an integer (`int`) and takes no arguments in this case.

### int num;

* Here, an integer variable named `num` is declared. This variable will store the number that the user enters for prime checking.

### bool isPrime = true;

* This line declares a boolean variable named `isPrime` and initializes it to `true`. This variable will be used to keep track of whether the input number is prime or not.

### printf("Enter a number: ");

* This line uses the `printf` function to display the message "Enter a number: " to the user.

### scanf("%d", &num);

* The `scanf` function is used to read input from the user. In this case, `%d` is a format specifier for an integer, and it expects the user to enter an integer. The value entered by the user will be stored in the `num` variable. The `&` operator is used to provide the memory address of the `num` variable so that `scanf` can store the value there.

### if (num <= 1) {

* This line starts an `if` statement. It checks whether the value stored in `num` is less than or equal to 1.

### isPrime = false;

* If the condition in the `if` statement is true (i.e., `num` is less than or equal to 1), this line sets the `isPrime` variable to `false`. It immediately marks the number as not prime.

### } else {

If the `if` condition is not met, this `else` statement is executed, indicating that `num` is greater than 1.

### for (int i = 2; i \* i <= num; i++) {

* Within the `else` block, a `for` loop is initiated. The loop variable is `i`, which is initialized to 2. The loop will continue as long as the square of `i` is less than or equal to the value stored in `num`. In each iteration, `i` is incremented by 1.

### if (num % i == 0) {

* Within the `for` loop, this line checks if the value of `num` is evenly divisible by the value of `i`. It checks for factors by calculating the remainder of the division.

### isPrime = false;

* If the condition in the `if` statement is true (i.e., `num` is divisible by `i`), this line sets the `isPrime` variable to `false`, indicating that the number is not prime.

### break;

* This `break` statement is executed if the number is found to be divisible by any value of `i`. It exits the `for` loop prematurely, as there's no need to continue checking for more factors once a factor is found.

### if (isPrime) {

* After the `for` loop, this `if` statement checks the value of the `isPrime` variable. If it is still `true,` it means no factors were found, and the number is prime.

### printf("%d is a prime number.\n", num);

* In this case, the `printf` function is used to display a message indicating that the value of `num` is a prime number. The values of `num` are inserted into the string where `%d` is used as the format specifier for integers.

### } else {

* If the `isPrime` variable is `false` (indicating that factors were found), this `else` statement is executed.

### printf("%d is not a prime number.\n", num);

* In this case, the `printf` function is used to display a message indicating that the value of `num` is not a prime number. The values of `num` are inserted into the string where `%d` is used as the format specifier for integers.

### return 0;

* The `return` statement is used to indicate that the `main` function is finished executing. It also returns an exit status of 0, which typically indicates that the program terminated successfully.