This code can be divided into the following parts for analysis:
-
Function definition part:
int app(int a,int b) { if(b==0) { return a; } else { return app(b,a%b); } }
This part defines a function named
app
, which is used to calculate the greatest common divisor of two integers. It uses a recursive implementation of Euclidean's algorithm. The function accepts two integer parametersa
andb
. Ifb
is equal to 0, it returnsa
as the greatest common divisor; otherwise call theapp
function recursively, passingb
as a new parameter toa
, < a i=9> is passed to as a new parameter until the recursion termination condition is met.a%b
b
-
Main function part:
int main() { int a,b,sum=0; printf("输入两个数字,(以空格为分隔符):"); scanf("%d %d",&a,&b); sum=app(a,b); printf("最大公约数为:%d",sum); return 0; }
This part is the entry function of the program
main
, and the program starts execution from here. Three integer variablesa
,b
andsum
are defined in the main function, which are used to store the two numbers entered by the user. and the greatest common divisor. Prompt the user for two numbers by using theprintf
function, and read the two numbers from the user input using thescanf
function. Then call theapp
function to calculate the greatest common divisor and assign the result to thesum
variable. Finally, use theprintf
function to output the value of the greatest common divisor. The program ends with thereturn 0
statement, indicating that the program has been executed normally.
The function of this code is to calculate and output the greatest common divisor of two numbers. The user needs to input two numbers when the program is running, and then the program calculates the greatest common divisor of the two numbers by calling the app
function and outputs the result to the user.
This code is a complete program to find the greatest common divisor of two integers.
In the main function, two integer variables a
and b
are first defined, and then the scanf
function is used to obtain the Read these two numbers from the user input.
Next, call the app
function to calculate the greatest common divisor and save the result in the sum
variable.
Finally, use theprintf
function to output the value of the greatest common divisor.
This code runs fine and calculates the greatest common divisor of two numbers entered by the user and outputs the result to the user.
#include<stdio.h>
int app(int a,int b)
{
if(b==0)
{
return a;
}
else
{
return app(b,a%b);
}
}
int main()
{
int a,b,sum=0;
printf("输入两个数字,(以空格为分隔符):");
scanf("%d %d",&a,&b);
sum=app(a,b);
printf("最大公约数为:%d",sum);
return 0;
}