【c语言】 从键盘输入一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)。要求在输入数据后对其进行检查是否小于1000的正数。如不是,则要求重新输入

第一种

#include <stdio.h>

#include<math.h>

int main()

{

float x, y;

printf("输入");

scanf_s("%f", &x);

while (x >= 1000) //当数字大于1000时,就会重新输入,直到输入的数小于1000.

{

printf("请重新输入");

scanf_s("%f", &x);

}

y = sqrt(x);

printf("y=%f", y);

return 0;

}

第二种

#include<stdio.h>

#include<math.h>

int main()

{

float a,b;

while(100)

{

printf("请输入一个数字:\n");

scanf_s("%f", &a);

if (a > 0&&a<=1000)

{

b = sqrt(a);

printf("根为%5.2f", b);

}

else

printf("请重新输入:");

}

return 0;

}

第三种

#include<stdio.h>

#include<math.h>

int main()

{

float a,d;

int c,b=1000;

printf("请输入一个数字:");

while (10)

{

scanf_s("%f", &a);

c = a / b;

d = sqrt(a);

switch (c)

{

case 0:printf("您的根为%5.2f", d); break;

default:printf("请重新输入:");

}

}

return 0;

}

第二种比第一种好在利用了while循环语句,可以实现多次进行输入。

第三种比第二种又加了一个switch

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值