Possible Solutions
With for-loop
#include <stddef.h> #include <stdio.h> #define N 6 size_t factorial(size_t n) { size_t res = 1; for (size_t i=2; i<=n; ++i) { res *= i; } return res; } int main() { printf("result = %zu\n", factorial(N)); return 0; }
With while-loop
#include <stddef.h> #include <stdio.h> #define N 6 size_t factorial(size_t n) { size_t res = 1; size_t i=2; while (i<=n) { res *= i; ++i; } return res; } int main() { printf("result = %zu\n", factorial(N)); return 0; }
With goto and if statements
#include <stddef.h> #include <stdio.h> #define N 6 size_t factorial(size_t n) { size_t res = 1; size_t i=2; goto while_check; while_loop: res *= i; ++i; while_check: if (i<=n) { goto while_loop; } return res; } int main() { printf("result = %zu\n", factorial(N)); return 0; }