函数练习

函数练习

(1)自定义函数实现平方的算法,相当于库函数pow();的效果。

#include<stdio.h>
//#include<math.h>;
double power(double x,double y);//声明函数
void main(){
	double x= 2.0,y =3.0, z;
	z = power(x,y);
	printf("%.1f to the power of %.1f  is %.1f\n",x,y,z);
}

double power(double x, double y)//定义函数
{
	double z = 1;//z接收xy运算的值
    while (y)
	{
		z *=x;
		--y;
	}
	return z;//返回z
}

(2)實現平方根計算。

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

int sqrt_02(int question);//the prespeaking of the funtion
void main(){

	int question = 49,answer;  
	answer = sqrt_02(question);

	if(answer < 0)
	{
		printf("错误:sqrt returns %d\n ,answer");
	}else
	{
		printf("The square root of %d is %d\n",question,answer);
	}
  
}


int sqrt_02(int question)
{
	int temp = question/2;
	while(temp--)
	{
		if (temp *temp == question)
		{
			 return temp;
		} 
	}

}

(3)编写一个统计各个数字,空白符(空格,制表符,换行符),以及所有其他字符出现次数的程序。
分别存储在变量number[10],blank,onter里面并打印出来

(4)阶乘算法实现


//这是个错误的答案,我明天再来改
#include<stdio.h>
int jiecheng(int number);
void main(){
int number = 6;
int answer;
answer = jiecheng(number);
printf("%d\n",answer);
}

int jiecheng(int number)
{
	int j=0;
	int i =1;
/*	for(int i =1;i<=number,i++ )
	{
		result += i*i;
	}
	*/

	while (j<=number)
	{
		i = i*(j+1);
		j+=1;
	}
	return i;
}
  20210331,明天愚人节快乐
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值