write a recursive method that uses only addition subtraction

write a recursive method that uses only addition, subtraction and comparison to multiply two numbers.

Solution

int multiply ( int n , int m)

{

if ( n == 0)

  return 0 ;

else if (n < 0 )

  return multiply (n ,m) ;

else if ( n == 1 )

return m;

else if ( n <= m)

return multiply ( n1,m)+m;

else return multiply(m, n ) ;

}

rhe driver code will be like

while ( 1 )

{

cout << ”\ Enter 2 nos to multiply ,

or 00 to qui t : ” << endl ;

ci n >> j >> k ;

if ( j == 0 && k == 0 ) break ;

else cout << multiply(j ,k ) ;

}

write a recursive method that uses only addition, subtraction and comparison to multiply two numbers.Solutionint multiply ( int n , int m) { if ( n == 0) return

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site