1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Recursive function to find X to the power n

Discussion in 'C' started by rashida.par, Mar 13, 2008.

  1. rashida.par

    rashida.par New Member

    Joined:
    Feb 14, 2008
    Messages:
    21
    Likes Received:
    1
    Trophy Points:
    0

    Introduction



    In this i have made use of recursion to calculate x to the power n

    Code:
    
    #include <stdio.h>
    
    main()
    {
    	float x;
    
    	int n;
    
    	printf("\n enter values of x an n");
    
    	printf("\n %.4f to the power of % d is %.4f",x,n,power(x,n)); 
    }
    Here power is a user defined function the job of this function is to check the values of x and n and either call the function recursively or return back.

    Code:
    
    float  power(float x, int n)
    {
    
    	if (x==0) 
    	{
    		return 0;
    
    	}
    	else if(n==0)
    	{
    		return 1;
    
    	}
    	else if (n>0)
    	{
    		return( x* power(x,n-1));
    	}
    	else
    	{
    		return ((1/x)*power(x,n+1));
    	}
    }
     
    shabbir likes this.
  2. ismail.tech21

    ismail.tech21 New Member

    Joined:
    Feb 14, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
  3. rai_gandalf

    rai_gandalf New Member

    Joined:
    Nov 4, 2005
    Messages:
    46
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Final Year Comp Engg
    Location:
    Mumbai
    Home Page:
    This is a simple implementation of a standard algorithm & is a classic example of Tail Recursion - the form of recursion which is redundant & can be replaced by a simpler, more efficient iterative equivalent (as Pradeep has done in his article of the same program - click here)


    Regards,
    Rajiv Iyer


    PS: Tail Recursion's classic trademark identification signature is that the recursive calls are usually the last statements within the recursive function's body.
     
  4. GAURAV SABOO

    GAURAV SABOO New Member

    Joined:
    Apr 13, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    i cannot understand this.please do it in an easy way
     
  5. GAURAV SABOO

    GAURAV SABOO New Member

    Joined:
    Apr 13, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    thank you rashida.you have done it in a very easy way
     
  6. mikhala

    mikhala New Member

    Joined:
    Apr 27, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    How do you do this manually in C++ without using the Power functionality?
     
  7. wagmare

    wagmare New Member

    Joined:
    May 14, 2008
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    soft.engg
    Location:
    Bangalore
    thats excellent especially the negative sides.... can u help me for me to learn using recursion in C
     
  8. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA

Share This Page