From ec4c8006080eedc263fef4d43f66ba84de10c08b Mon Sep 17 00:00:00 2001 From: Jeyell26 <80738021+Jeyell26@users.noreply.github.com> Date: Sat, 17 Apr 2021 11:33:10 +0800 Subject: [PATCH] Add files via upload --- factorial.c | 11 ++++++++--- fibonacci.c | 14 ++++++++++---- sum.c | 9 +++++++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/factorial.c b/factorial.c index a7bf980..5d11f67 100644 --- a/factorial.c +++ b/factorial.c @@ -8,7 +8,12 @@ */ int factorial (int n) { // ADD CODE HERE - return 0; // EDIT THIS + int i, factorial; + factorial = 1; + for (i=1;i<=n;i++){ + factorial = factorial * i; + } + return factorial; // EDIT THIS } @@ -16,7 +21,7 @@ int factorial (int n) { int main(void) { int k=5; - + printf("%d! = %d\n", k,factorial(k)); k = 8; printf("%d! = %d\n", k,factorial(k)); @@ -24,4 +29,4 @@ int main(void) { printf("%d! = %d\n", k,factorial(k)); return 0; -} \ No newline at end of file +} diff --git a/fibonacci.c b/fibonacci.c index b5e464d..d75f053 100644 --- a/fibonacci.c +++ b/fibonacci.c @@ -17,9 +17,15 @@ * fibonacci(8); //21 */ int fibonacci(int k) { - //WRITE CODE HERE - - return 0; // EDIT THIS + int t1 = 0, t2 = 1, nextTerm = 0; + int i; + for (i = 1; i < k; i++) { + // Prints the first two terms. + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + return nextTerm; // EDIT THIS } @@ -33,4 +39,4 @@ int main(void) { printf("Fibonacci at %d is %d\n", k, fibonacci(k)); return 0; -} \ No newline at end of file +} diff --git a/sum.c b/sum.c index 0ee167a..8334c61 100644 --- a/sum.c +++ b/sum.c @@ -14,9 +14,14 @@ * series(3,6); // returns 18. coz, 3 + 4 + 5 + 6 */ int series(int a, int b){ + int sum; //Write code here + int i; + for (i=a;i<=b;i++){ + sum = sum + i; + } - return 0; //EDIT THIS + return sum; //EDIT THIS } @@ -30,4 +35,4 @@ int main(void) { printf("The sum from %d to %d is %d\n",a,b,series(a,b)); return 0; -} \ No newline at end of file +}