C语言程序设计之数学函数篇1

问题1_1

        函数 f u n fun fun 的功能是计算:
s = ln ⁡ ( 1 )   +   ln ⁡ ( 2 )   +   ln ⁡ ( 3 )   +   ⋯   +   ln ⁡ ( n )   s=\sqrt{\ln(1)\ +\ \ln(2)\ +\ \ln(3)\ +\ \cdots \ +\ \ln(n)\ } s=ln(1) + ln(2) + ln(3) +  + ln(n) 
         s s s 作为函数值返回。

代码1_1

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

double fun(int m){
	int i;
	double s=0.0;
	for(i=1; i<=m; i++)
		s = s+log(i);
	return sqrt(s);
}

void main(void){
	system("CLS");
	printf("%f\n", fun(20));
}

结果1_1

Result_!_!

问题1_2

        函数 f u n fun fun的功能是:求三个数的最小公倍数。

代码1_2

#include<stdio.h>

int fun(int x, int y, int z){
	int j, t, n, m;
	j = 1;
	t = j%x;
	m = j%y;
	n = j%z;
	while(t!=0||m!=0||n!=0){
		j = j+1;
		t = j%x;
		m = j%y;
		n = j%z;
	}
	return j;
}

void main(void){
	int x1, x2, x3, j;
	printf("Input x1 x2 x3:");
	scanf("%d %d %d", &x1, &x2, &x3);
	printf("x1 = %d, x2 = %d, x3 = %d\n", x1, x2, x3);
	j = fun(x1, x2, x3);
	printf("The minimal common mltiple is:%d\n", j);
}

结果1 _2

Result_1_2

问题1_3

         函数 f u n fun fun的功能是:用递归算法求形参 a a a 等待的平方根 。求平方根的迭代公式如下 。
         x 1   =   1 2 ( x 0   +   a x 0 ) x_{1}\ =\ \frac{1}{2}(x_{0}\ +\ \frac{a}{x_{0}}) x1 = 21(x0 + x0a)
        迭代步骤如下:
         例如: a a a 2 2 2 时,平方根值为 1.414214 1.414214 1.414214

代码1_3

#include<math.h>
#include<stdio.h>

double fun(double a, double x0){
	double x1, y;
	x1 = (x0+a/x0)/2.0;
	if(fabs(x1-x0)>=0.00001)
		y = fun(a, x1);
	else
		y = x1;
	return y;
}

void main(void){
	double x;
	printf("Enter x:");
	scanf("%lf", &x);
	printf("The square root of %lf is %lf\n", x, fun(x, 1.0));
}

结果1_3

Function_1_3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值