#include <stdio.h>

/*
******************************************************
Pre-sessional learning for iBSc students
Programming and electronics, Section 3, Extension task
******************************************************

Note:
    Although this script is prepared in C, the code defined in the main() function
    should be compatible with the Arduino-specific language (which is a variant of C).
    To execute, you need to carefully integrate the code defined in main() to your
    Arduino IDE.

Version history:
    Date            Programmer      Description
    ----------      ----------      ----------------------------
    23/08/2024      B LI            create
*/

int main(){
// implement the following to your Arduino IDE


/*
    you may have noticed that the data type for the variable n_factorial is
    **unsigned long long int** rather than **int**. Why?

    The reason here is, an int type  variable takes 4 bytes (1 byte = 8 bits,
    hence 4 bytes = 32 bits); and, by default, int is **signed** (can be + or -)
    by default. This means, an int variable must within the range [-2^31 to 2^32-1].
    Any values outside this range will cause **overflow** (hence, a wrong result).

    To mitigate such issues, we use **unsigned long long int**, which takes 8 bytes
    (8 bytes = 64 bits) and limit the variable to represent only non-negative numbers.
    Hence, the max value it can represent is 2^64 - 1, which will tolerate a larger
    number.
*/
    int n;
    unsigned long long int n_factorial;

    n = 20;
    n_factorial = n;

    while (n > 1){
        n -= 1;
        n_factorial *= n;
    }

    // note: %llu is the format specifier for unsigned long long int variables.
    printf("n! = %llu \n", n_factorial);

    return 0;
}



