Q:

C++ program to obtain Multiplication recursively

belongs to collection: C++ programs on various topics

0

C++ program to obtain Multiplication recursively

Given two integers m and ncalculate and return their multiplication using recursion. You can only use subtraction and addition for your calculation. No other operators are allowed.

Input format: m and n (in different lines)

    Sample Input:
    3 
    5

    Sample Output:
    15

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Algorithm:

  1. To solve using recursion, define a recursion function with 2 parameters m and n (the numbers you want to multiply).
  2. Base Case: if n==0 then return 0.
  3. return m + recursive call with parameters m and n - 1.

C++ Code/Function:

#include <bits/stdc++.h>
using namespace std;

int multiplyTwoInteger(int m, int n){
	if(n == 0){
		return 0;
	}
	return m + multiplyTwoInteger(m,n-1);
}

int main(){
	int m = 3, n =5;

	cout<<multiplyTwoInteger(m,n);

	return 0;
}

Output

15

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to check whether a given Binary Search... >>
<< C++ program to find Last occurrence of a Number us...