Saturday, August 20, 2011

Efficient code to find exponential(power) of given number using recursion.

//Efficient code to find exponential(power) of given number using recursion.
#include<stdio.h>
#include<conio.h>
main()
{
      int n,p;
      int exp(int n,int p);
      printf("Enter the number :");
      scanf("%d",&n);
      printf("Enter the power you want to compute :");
      scanf("%d",&p);
      printf("\n the term is %d",exp(n,p));
      getch();
      return 0;
}
int exp(int n,int p)
{
    if(p==0)
    return 1;
if(p%2==0)
return exp(n*n,p/2);
else
return (exp(n*n,p/2)*n);
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More