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

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);
}

Lol Rofl

thanks saad749 :)

Most Welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.