Current location - Education and Training Encyclopedia - University rankings - How to find the square or cube of variables in C language
How to find the square or cube of variables in C language
Can be done recursively:

# include & ltstdio.h & gt

Double factorial (integer)

{

return n & gt0 ? N * factorial (n-1):1;

}

Double power (int x, int y)

{

If (y>0)

return y & gt0 ? X * power (x, y-1):1;

other

return 1/(y & lt; 0 ? X * power (x, (-y)-1):1);

}

int main()

{

Double a = power (3,5); // 3^5

Double b = power (3,-5); // 3 ^-5

Double fac = factorial (10); // 10! = 10 * 9 * 8 *````* 2 * 1

printf(" 10! = %lf\n ",fac);

Printf("3 to the 5th power: %lf\n ",a);

Printf("3 to the fifth power: %lf\n ",b);

}

There is no symbol defining factorial in the computer, but an exclamation point in mathematics, such as 5! Equal to 5 * 4 * 3 * 2 * 1, so equal to 120.