Recursion Program in Java

Write a program in java to demonstrate the Recursion

 
public class RecursionDemo 
{ 
 int power(int n,int m)
 {
  if((m==-1)||(m==1))
  {
   return n;
  }
  else
  {
   return n*power(n,m-1);
  }
 }
 int mul(int n,int m)
 {
  if(m==1)
  {
   return n;
  }
  else
  {
   return n+mul(n,m-1);
  }
 }
 public static void main(String[] args) 
        {
  RecursionDemo r=new RecursionDemo();
  System.out.println("2 ^ 3 = "+(r.power(2, -1)));
  System.out.println("2 * 3 = "+(r.mul(2, 3)));
 }
}

Output

0 comments:

Post a Comment