编写c语言程序,从键盘读取任意一个非0正整数x,判断这个教是否是平方数,如果是,则输出这个数的平方根,如果不是则输出0
时间: 2024-06-05 16:06:21 浏览: 8
#include <stdio.h>
#include <math.h>
int main() {
int x;
printf("请输入一个非0正整数x:");
scanf("%d", &x);
int sqrt_x = sqrt(x);
if (sqrt_x * sqrt_x == x) {
printf("%d是一个平方数,其平方根为%d\n", x, sqrt_x);
} else {
printf("0\n");
}
return 0;
}
相关问题
用C语言编写程序,从键盘读取任意一个非0正整数x,判断这个数是否是平方数,如果是,则输出这个数的平方根,如果不是,则输出0
```c
#include <stdio.h>
#include <math.h>
int main() {
int x;
printf("请输入一个非0正整数:");
scanf("%d", &x);
int s = sqrt(x); // 计算平方根
if (s * s == x) { // 判断是否是平方数
printf("%d的平方根是%d\n", x, s);
} else {
printf("0\n");
}
return 0;
}
```
编写c语言代码,从键盘读取任意一个非0正整数x,判断这个数是否是平方数,如果是,则输出这个数的平方根,如果不是,则输出0。
```c
#include <stdio.h>
#include <math.h>
int main() {
int x;
printf("请输入一个非0正整数:");
scanf("%d", &x);
if (x <= 0) { // 判断输入是否是非0正整数
printf("输入错误!\n");
return 0;
}
int s = sqrt(x); // 计算平方根
if (s * s == x) { // 判断是否是平方数
printf("%d的平方根为%d\n", x, s);
} else {
printf("0\n");
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)