Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Factorial_Function by
Diff: main.cpp
- Revision:
- 0:c8831b97b76a
- Child:
- 1:e6e33adba30e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri Sep 22 15:48:50 2017 +0000
@@ -0,0 +1,39 @@
+/* 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);
+
+ }
+ }
+ }
+}
\ No newline at end of file
