The method of writing x to the power of y in c program: first create a new project file in c language; then define two parameters n and k, which represent the input number and the required power number respectively; and finally use recursion The calculation to the nth power can be completed by multiplying n by the decreasing k.
The operating environment of this article: windows7 system, Dell G3 computer/C11.
How to write x to the power of y in c program?
Create a new C language project file and introduce the header file. Here, first define a poetry function to process the calculation of x raised to the yth power. Define two parameters n and k, which represent the input number and the required times respectively. Power number:
In the Poewer function, consider the situation where the k-th power is 1 and 0, and then use a recursive method to multiply n by the decreasing k to instantly complete n Calculation of powers. Then define the input data in the main function, use the scanf function to accept the value, call the power function to calculate, and finally output the result:
Run the program, first enter the number 5, Press Enter, then enter the number 6 and press Enter. Finally, the program prints the result of 5 raised to the power of 6. The above is a demonstration of using C language to find the Y power of X:
Recommended: "c Language Tutorial"
The above is the detailed content of How to write x raised to the yth power in c program. For more information, please follow other related articles on the PHP Chinese website!