C语言编程,函数
#include <stdio.h>
#include <conio.h>
/* 辗转相除法求最大公约数 */
int gcd (int m, int n) {
if (n)
while((m %= n) && (n %= m)); /* 两数反复相除取余,直至被整除 */
return m+n;
}
int lcm (int m, int n) {
return m * n / gcd (m, n);
}
int main (void) {
int m, n;
int gcdnum, lcmnum; /* 最大公约数、最小公倍数 */
m = 252; n = 105; /* 最大公约数为21,最小公倍数为1260 */
gcdnum = gcd (m, n);
lcmnum = lcm (m, n);
printf ("%d %d\n", m, n);
printf ("The gcd is %d\n", gcdnum);
printf ("The lcm is %d\n", lcmnum);
getch (); /*屏幕暂留*/
return 0;
}
#include <stdio.h>
float max(float a[])
{
int i;
float m = a[0];
for(i=1;i<10;i++)
{
if(m<a[i])
m = a[i];
}
return m;
}
float min(float a[])
{
int i;
float m = a[0];
for(i=1;i<10;i++)
{
if(m>a[i])
m = a[i];
}
return m;
}
float aver(float a[])
{
float sum = 0;
int i;
for(i=0;i<10;i++)
sum += a[i];
return sum / 10;
}
int main()
{
float num[10];
int i;
for(i=0;i<10;i++)
scanf("%f",&num[i]);
printf("The max number is %f\n",max(num));
printf("The min number is %f\n",min(num));
printf("The average number is %f\n",aver(num));
return 0;
}