Write a C program to calculate pow(x,n)?

Method 1 :

int pow(int x, int y)
{
if(y == 1) return x ;
return x * pow(x, y-1) ;

}
Method 2 :

Divide and Conquer C program

#include <stdio.h>
int main(int argc, char*argv[])
{
printf(“\n[%d]\n”,pow(5,4));
}

int pow(int x, int n)
{
if(n==0)return(1);
else if(n%2==0)
{
return(pow(x,n/2)*pow(x,(n/2)));
}
else
{
return(x*pow(x,n/2)*pow(x,(n/2)));
}
}

Advertisements

One thought on “Write a C program to calculate pow(x,n)?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s