MSOE EE2905 / Mbed 2 deprecated Factorial_Function

Dependencies:   mbed

Fork of Factorial_Function by Sheila Ross

main.cpp

Committer:
rossatmsoe
Date:
2017-09-22
Revision:
0:c8831b97b76a
Child:
1:e6e33adba30e

File content as of revision 0:c8831b97b76a:

/*  Factorial

Demonstrates the use of a for loop to compute a factorial, and use of scanf

Turn on local echo in your terminal application to see what you have typed

*/

#include "mbed.h"

Serial pc(USBTX,USBRX);

int n;
int main()
{
    while(1) {

        pc.printf("Enter the number (0 through 12):\n");
        pc.scanf("%d",&n);

        if(n<0) {
            pc.printf("Error:  Number must be non-negative\n");
        } 
        else {
            if(n>12) {
                pc.printf("Error:  Number is too large\n");
            } 
            else {
                
                unsigned long answer=1;
                for(int k=1; k<=n; k++) {
                    answer*=k;
                }
                pc.printf("%d!=%lu\n",n,answer);

            }
        }
    }
}