Skip to main content

Basic question on recursion

Examples on Recursion.

Sum of natural number using Recursion.
public class SumOfNaturalNumber {
  public static int sum(int x){
     if(x<=0)
       return 0;
    return sum(x-1)+x;
  }
  public static void main(String[] args) {
      System.out.println(sum(20));
  }
}
Factorial of given number.
public class FactorialOfNumber {
  public static int factorial(int x){
     if(x==0)
       return 1;
    return factorial(x-1)*x;
  }
  public static void main(String[] args) {
      System.out.println(factorial(20));
  }
}
Find the power of the given number.
public class PowerOfNumber {
/*First method*/
  public static int Power(int base,int exponent){
     if(exponent==0)
       return 1;
    return factorial(base,exponent-1)*base;
  }
 /*second method*/
  public static int Power1(int base,int exponent){
         if(exponent==0)
            return 1;
         if(exponent%2==0)
            return Power1(base*base,exponent/2);
        return base*Power1(base*base,(exponent-1)/2);
  }
  public static void main(String[] args) {
                   //input Power(base,exponent)
      System.out.println(Power(4,2));
     System.out.println(Power1(4,2));
  }
}


Taylor Series using recursion.
public class Exponential {
    static int power=1;
    static int factorial=1;
  public static double exponential(int x,int n){
     double r;
     if(n==0)
       return 1;
     r=exponential(x,n-1);
     power=power*x;
     factorial=factorial*n;
     return r+power/factorial;
  }
  public static void main(String[] args) {
      System.out.println(exponential(1,10));
  }
}
Factorial Of Given Number.
public class FactorialOfNumber {
  public static int factorial(int x){
     if(x==0)
       return 1;
    return factorial(x-1)*x;
  }
  public static void main(String[] args) {
      System.out.println(factorial(20));
  }
}


Taylor Series using Horner's rules.
public class TaylorSeriesOnHornerRule {
  static int s=1;
  public static double exponential(int x,int n){
     if(n==0)
       return s;
    s=1+x*s/n;
    return exponential(x,n-1);
  }
  public static void main(String[] args) {
      System.out.println(exponential(2,10));
  }
}


Find the nth term from Fibonacci series.
public class FibonacciSeries {
  public static int fibonacciTerm(int x){
     if(x==0)
       return 0;
     if(x==1)
        return 1;
    return fibonacciTerm(x-1)+fibonacciTerm(x-2);
  }
  public static void main(String[] args) {
      System.out.println(fibonacciTerm(20));
  }
}


Find the Combination using nCr
public class Combinations {
  public static int ncr(int n,int r){
     if(n==r || r==0)
       return 1;
    return ncr(n-1,r-1)+ncr(n-1,r);
  }
  public static void main(String[] args) {
      System.out.println(ncr(5,2));
  }
}


Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Operators & Assignment part 3

                                                                                       Operators & Assignment 1-:instanceof Operators 2-:Bitwise Operators                                                                        instanceof Operators we can use instanceof operator to check whether the given object is of a particular type are not. Example-: /* list is a collection of objects. List l=new List(); l.add(Customer); l.add(Student); l.add(Test); Object o=l.get(...