Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 148,635 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,386 people online right now. Registration is fast and FREE... Join Now!





Multiplying numbers using Double-recursion

This multiply recursive function uses a recursive add function to get the product of two numbers.

Submitted By: born2c0de
Actions:
Rating:
Views: 2,404

Language: C++

Last Modified: July 13, 2008

Snippet


  1. /*
  2.      Written by Sanchit Karve AKA born2c0de
  3. */
  4.  
  5. #include <iostream>
  6.  
  7. int sum(int x,int y)
  8. {
  9.      if(!y)
  10.           return x;
  11.  
  12.      return sum(x+1,y-1);
  13. }
  14.  
  15. int multiply(int x,int y)
  16. {
  17.      if(y==1)
  18.           return x;
  19.  
  20. //     return x + multiply(x,y-1); // Using only Multiply Function
  21.    return sum(x,multiply(x,y-1)); // Using 2 Rec. Functions
  22. }
  23.  
  24.  
  25.  
  26. int main()
  27. {
  28.     cout<<multiply(19,5);
  29.    return 0;
  30. }

Copy & Paste


Comments


venkatritch 2008-06-29 08:29:19

int xy(int x, int y) { if(x==1) return y; else return( y+ xy(x-1,y)); } This will also does. Venkat www.ritchcenter.com/elearn

born2c0de 2008-06-30 11:55:55

I am aware of that. I simply wanted to demonstrate double-recursion.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month