函数练习
(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,明天愚人节快乐