Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions factorial.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
*/
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
}



int main(void) {

int k=5;

printf("%d! = %d\n", k,factorial(k));
k = 8;
printf("%d! = %d\n", k,factorial(k));
k = 10;
printf("%d! = %d\n", k,factorial(k));

return 0;
}
}
14 changes: 10 additions & 4 deletions fibonacci.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand All @@ -33,4 +39,4 @@ int main(void) {
printf("Fibonacci at %d is %d\n", k, fibonacci(k));

return 0;
}
}
9 changes: 7 additions & 2 deletions sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand All @@ -30,4 +35,4 @@ int main(void) {
printf("The sum from %d to %d is %d\n",a,b,series(a,b));

return 0;
}
}