Pages

Saturday, June 29, 2013

Recursive function for multiplication of two numbers using addition in C++

/* QUESTION :

Write a function int Mul(int a,int b) that computes the product of two integers a and b. THE ONLY ARITHMETIC OPERATION THAT YOU ARE ALLOWED TO USE IS ADDITION '+' USING RECURSION
*/


// CODE :
#include <iostream>
using namespace std;

int Mul(int a,int b);
//MAIN STARTS
int main(){
int a,b;
cout<<"Enter first number : ";
cin>>a;


cout<<"\nEnter second number : ";
cin>>b;

cout<<"\n\nTHE RESULT AFTER MULTIPLICATION IS : "<<Mul(a,b)<<endl;
 return 0;
}

//MAIN ENDS
int Mul(int a , int b){
if (b==0)
return 0;

if (a==0)
return 0;

else
return (a + Mul(a,b-1));
}

 

No comments:

Post a Comment