[Functions]D. Liang 5.19 Using the sqrt function

Description
Input two integers A and B (0<A,B<1000, A<B), then prints the square root of all integers between A and B (including A and B).

Example Input
0 5
Example Output

 Number  SquareRoot
           0      0.0000
           1      1.0000
           2      1.4142
           3      1.7321
           4      2.0000
           5      2.2361

*The last line is a newline character.

You should set the width of print field of each column to 12, set the precision of floating-point numbers to 4 and fixed, justify the output to the right.

作业记录。
这题比较坑的是sqrt要直接塞进printf里面输出不然随机测试会给你弄点莫名其妙的误差出来。
凡是随机测试出现偶尔有最后一个位的数误差,
1.可以更改数据类型,比如double,float等试试。
2.可以新定义一个变量,初始化为要输出的值,或者直接将要输出的值(比如直接整函数返回值、某个计算公式)放在printf里面输出。
3.有时候用pow函数做幂运算和用循环做幂运算的结果会产生些许不同。
4.……

#include<stdio.h>
#include<math.h>
int main(){
	int a,b;
	scanf("%d %d",&a,&b);
	printf("      Number  SquareRoot\n");
	for(int i=a;i<=b;i++){
		printf("%12d%12.4f\n",i,sqrt(i));
	}
	printf("\n");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值