Saturday, 4 December 2010

function a power b without using pow() function

//function a power b without using pow() function
#include<stdio.h>
#include<conio.h>
long double power(float a,int b)
{
 long double prod=1;
 int i;
 for(i=1;i<=abs(b);i++)

  prod=prod*a;

 if(b>=0)//if power(b) is +ve
  return(prod);
 else
  return(1/prod);//if power is -ve
  }
 void main()
 {
  float a;
  int b;
  long double power(float a,int b);
  printf("enter base and power\n");
  scanf("%f%d",&a,&b);
  printf("power result is %Lf\n",power(a,b));
  getch();
 }
/*
enter base and power:
2
3
8.000000

Note:
 to find a power b power c:, function call is power(a,power(b,c))
*/

 Functions
programs on functions

No comments:

Post a Comment