Thursday, January 9, 2014

Recursive Function call at java

// Recursive Function call
// function call to itself
class recursive
{
  public static int sum(int n)
  {
    if(n==1)
      return 1;
    else
      return n+sum(n-1);
  }
  public static void main(String args[])
  {
    int a;
    a=sum(10);
    System.out.println(" Sum of 1 to 10 is "+a);
  }
}

No comments:

Post a Comment