blogger Indonesia

Follow judhyns blog

Recursion


RECURSION

Introduction

You can express most of the problems in the following program by using recursion. We represent the function add by using recursion.

Program

#include  int add(int pk,int pm); main() {     int k ,i,m;     m=2;     k=3;     i=add(k,m);.     printf("The value of addition is %d\n",i); } int add(int pk,int pm) {     if(pm==0) return(pk);          \\ A     else return(1+add(pk,pm-1));   \\ B } 

Explanation

  1. The add function is recursive as follows:

    add (x, y) = 1 + add(x, y-1)      y > 0        = x                   y = 0 for example,     add(3, 2) = 1 + add(3, 4)     add(3, 1) = 1 + add(3, 0)     add(3, 0) = 3     add(3, 1) = 1+3 = 4     add(3, 2) = 1+4 = 5 
  2. The recursive expression is 1+add(pk, pm-1). The terminating condition is pm = 0 and the recursive condition is pm > 0.


No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |