Question: Given two integers M & N, calculate and return their multiplication using recursion. You can only use subtraction and addition for your calculation. No other operators are allowed.
Input format:
Line 1: Integer M
Line 2 : Integer N
Ans:
Yaha ham kya kar rhe n == 1 ho jata hai to return kar de rhe m
example n =1
m = 4
1 X 4 = ? Answer will be 4 so return m
m+ recursive call m aur n-1
isme m hai 4 aur +4 and n-1 ka mtlb hai kitne bar recursive call karna hai
public class practiceRecursion {
public static int multiply(int n, int m){
if(n == 1){
return m;
}
return m+multiply(m,n-1);
}
public static void main(String [] agrs){
System.out.println(multiply(3,4));
}
}
Comments
Post a Comment