Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog

Programming Source Codes
C/C++ Source Codes
C# Source Codes
.........More

Aptitude Interview Questions
C/C++ Aptitude Questions
C Aptitude Questions
.........More

Tutorials
C Tutorial
C++ Tutorial
.........More

# C/C++ Source Codes

factorial using recursion and iteration/loops in C language

#include<stdio.h>

int fib_rec(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fib_rec(n - 1) + fib_rec(n - 2));
}

int fib_loop(int n) {
int a, b, sum;
a = b = sum = 0;
b = 1;
if (n <= 0)
return 0;
else if(n == 1 '' n == 2)
return 1;
while(n > 1) {
sum = a + b;
a = b;
b = sum;
n--;
}
return sum;
}

int main() {
int i, n = 37;
printf("the fib upto %d using recursion is: \n",n);
for(i = 0; i < n; i++)
printf("%d, ",fib_loop(i));

printf("\nthe fib upto %d using loop is: \n",n);
for(i = 0; i < n; i++)
printf("%d, ",fib_rec(i));

puts("");
return 0;
}

<<<----- Return to C/C++ Source Code Questions Page.

Have a Question ? post your questions here. It will be answered as soon as possible.

Check Object Oriented Interview Questions for more Object Oriented Interview Questions with answers

Check Data Structure Interview Questions for more data structure interview questions with answers