From 5d0f86545e5caf77248129af4d5fb7ae7d0b491d Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 12 Jan 2024 13:37:52 +0100 Subject: [PATCH] Add examples --- example/fibonacci_imp.c | 17 +++++++++++++++++ example/fibonacci_rec.c | 8 ++++++++ example/primes.c | 21 +++++++++++++++++++++ example/stack_trace.c | 13 +++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 example/fibonacci_imp.c create mode 100644 example/fibonacci_rec.c create mode 100644 example/primes.c create mode 100644 example/stack_trace.c diff --git a/example/fibonacci_imp.c b/example/fibonacci_imp.c new file mode 100644 index 0000000..86257ca --- /dev/null +++ b/example/fibonacci_imp.c @@ -0,0 +1,17 @@ +int num = 15; +int result = -1; + +if (num < 2) + result = num; +else { + int prev1 = 1; + int prev2 = 0; + for (int i=2; i < num; i++) + { + int tmp = prev1; + prev1 = prev1 + prev2; + prev2 = tmp; + }; + result = prev1+prev2; +} +result;; diff --git a/example/fibonacci_rec.c b/example/fibonacci_rec.c new file mode 100644 index 0000000..fa4c525 --- /dev/null +++ b/example/fibonacci_rec.c @@ -0,0 +1,8 @@ +int fibo(int n) { + if (n <= 1) { + return n; + } else { + return fibo(n - 1) + fibo(n - 2); + } +} +fibo(15);; diff --git a/example/primes.c b/example/primes.c new file mode 100644 index 0000000..032578b --- /dev/null +++ b/example/primes.c @@ -0,0 +1,21 @@ +int is_prime(int num) { + if (num <= 1) return 0; + for (int i = 2; i * i <= num; i++) { + if (num % i == 0) return 0; + } + return 1; +} + +int max_prime(int limit) { + int prime = 0; + + for (int i = 1; i < limit; i++) { + if (is_prime(i)) { + prime = i; + } + } + + return prime; +} + +max_prime(250);; diff --git a/example/stack_trace.c b/example/stack_trace.c new file mode 100644 index 0000000..4e946fc --- /dev/null +++ b/example/stack_trace.c @@ -0,0 +1,13 @@ +int function1(int a) { + return a/0; +} + +int function2(int b, int c) { + return function1(b+c); +} + +int main(int x) { + return function2(x, 2*x+3)*12; +} + +main(3);;