Jump to content

Using recursion to solve multiplication

- - - - -

  • Please log in to reply
12 replies to this topic

#1
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
The code below apply recursion to solve multiplication

Basically recursion is another way of performing loops with sometimes complex attributes to it. Since not all programming languages have the same syntax and logical sequencing and identifiers and not least keywords.

Recursion is used in programming languages where you can't use for loops and so on.

Recursion simply solves the loop problem in these languages.


In using recursion you first need to identify the base case as this will be your break in essence of stopping the recursion when it has achieved its purpose.

The recursive case is where you simply divide the more complex arguments for the repetitive section of code itself and this part needs to to do some critical thinking in solving such problems.





#include <stdio.h>

#include <conio.h>


int recursive(int,int); //Function Prototype and parameters


int main()

{

    int num,num2,ans;

    

    printf("Please enter the number:");

    scanf("%d",&num);

    

    printf("Please enter the multiplier:");

    scanf("%d",&num2);

    

    ans=recursive(num,num2);

    

    printf("The answer is:%d",ans);

    

    getch();

    return 0;

}


int recursive(int a,int b)

{

    if(b==0)//base case for recursion to stop

       return 0;

    else

       return a + recursive(a,b-1);//recursive case

       } 



#2
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
Most of people gets shocked when "Recursion" is mentioned, Yet once you understand it, It becomes simpler,easier than normal loops.

#3
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
i agree, its a more efficient method of looping in my eyes

#4
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
I don't get recursion :/

#5
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
Its like recursive functions in Math. F(x) =x * f(x-1 AND f(0) = 1 , If you applied the previous function to a number X, then you have done factorial recursively, Lets say x = 5. By evaluating F(5) we will get F(5) = 5 * f(4) = 5 * 4 * f(3) = 5 * 4 * 3 * f(2) = 5 * 4 * 3 * 2 * f(1) = 5 * 4 * 3 * 2 * 1 * f(0) = 5 * 4 * 3 * 2 * 1 * 1 = 120. Just note that F(0) = 1 Is the base case in which we want to stop the recursion when its reached, Just like conditions in loops.

Red Function to be evaluated.
Blue Elements that can be calculated without using the function eg: 5 * 4.
Green Elements that can't be evaluated without re-using the function.
Magneta The Base Case in which we will stop recursing.

#6
hbk

hbk

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
yeah a see what your saying, once you know the basics of math then you can just apply it to programming

#7
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Isn't recursion somewhat unreliable? C will segfault if recursion goes to deep. Python also has a limit on recursion depth. That's why I usually stick to while loops.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#8
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
Actually recursion is a concept more than a technique, And In some cases like dealing with Trees or Designing AI's. You have no other options other than using recursion (I guess).

#9
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
you can traverse a tree using loops but with simulating what happens when you call a function -using stack-, since a large enough tree will always end up crashing you C/C++ program when traversing using recursion, but other than crashing it's a good way to solve problems specially in Dynamic programming.
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#10
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
The only time I have used recursion in my business programs is when transversing a file system, such as iterating through directories and sub-directories. As a general rule of thumb recursion should be avoided when the programming language supports loops because (1) loops are executed a lot faster, and (2) they don't cost as much in computer resources. Programming recursion because its a cool thing to do and looks good in your program is no substitute for program efficiency and program maintainability.

Despite the severe limitations of recursion, this is a good tutorial to introduce the topic. Give it 10 stars :thumbup1:
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#11
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
Traversing a hierarchy of files and folders goes the same way as traversing a tree from root to leaves and the best way is recursion here which will take SO less code to do the job and maybe in some cases of different types of tree the code may not reach the base case. recursion doesn't solve everything, loops doesn't solve everything. One has to use the proper method that best suites the situation. But I'm still a big rec fan.

#12
Tonaldex

Tonaldex

    Newbie

  • Members
  • Pip
  • 8 posts
Nice job. Sense recursion is the same as using LABEL most expecially in pascal?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users