Friday, October 30, 2009

Power function

Write a function power ( a, b ), to calculate the value of a raised to b.



void main()
{
float power(float,float);
float x,y,z;
printf("\nInput the first value and second of which the first is raised to the power of the second value");
scanf("%f%f",&x,&y);
z=power(x,y);
printf("\nThe answer is %f",z);
}


float power(float a,float b)
{
float c;

if(b==0)
return(1);

if(b==1)
return(a);

else
c=a*power(a,b-1);

return(c);
}

2 comments:

  1. /*
    Progrm: Chap-5 (2-b)
    Write a function power ( a, b ), to calculate the value of a raised to b.

    31/10/2011
    */

    #include

    int power(int,int);

    void main()
    {

    int a,b,pow;
    clrscr();

    printf("\nEnter number: ");
    scanf("%d",&a);

    printf("\nEnter power of %d: ",a);
    scanf("%d",&b);

    pow=power(a,b);

    printf("\nThe value of %d raised to %d is: %d",a,b,pow);

    getch();
    }

    int power(int x, int y)
    {
    int f=1,i;

    for(i=1;i<=y;i++)
    f*=x;

    return(f);
    }

    ReplyDelete