Created
January 15, 2015 07:19
-
-
Save kenornotes/6f4a98503294bdd77bd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//recursion | |
int func(int n) { | |
if(n == 0) { | |
return 0; | |
} | |
if(n % 2 != 0) { | |
return n + func(n - 1); | |
} else { | |
return - n + func(n - 1); | |
} | |
} | |
//for-loop | |
int func2(int n) { | |
int i, sum = 0; | |
for(i = 1; i <= n; i++) { | |
if(i % 2 != 0) { | |
sum = sum + i; | |
} else { | |
sum = sum - i; | |
} | |
} | |
return sum; | |
} | |
int main() { | |
int n; | |
scanf("%d", &n); | |
printf("Recursive function = %d\n", func(n)); | |
printf("for loop = %d", func2(n)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment