0

hi guys! please help me understand anything about recursion and iteration ways of coding. :) (maybe some overview, details or something) thank u. we haven't had any further discussion about this. ;p but our teacher gave an exercise that goes like this:


make a program that gets the summation of a given number e.g. 4+3+2+1

implement it using both iteration and recursion

ex. enter a positive number: 4
the sum is 10.


make a program that would display the nth of the fibonnaci sequence.

1 1 2 3 5 8 13 21 34 55 ... n

implement it using both iteration and recursion

3
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by saad749
0

Iteration can be done by the help of loops, i.e. for loop, while loop, do .. while loop.

Recursion on the other hand means recursive calls to a function. The recursive calls are mostly conditional.

for example, taking your first program, by iteration it can be done by:

int sum=0;
for(int i = 4; i>0; i--)
{
   sum+=i;
}
printf("%d",i);

Recursion will require u to make a function call itself repeatedly until the condition is met.

int recur(int n)
{
   sum+=n;
   if(n>0)
      recur(n-1);
   return(sum);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.