从键盘输入一个整数,判断此数如果不满足“大于等于1000且小于等于9999”,就重新输入。将输入的4位整数的千位与个位交换,百位与十位交换,并输出新得到的数字。如输入1204,输出4021;输入1240,输出421。在下列给出的代码中补充语句,完成以上功能,并将运行结果窗口截图粘贴在程序下方。要求不修改原始代码。 #include "stdio.h" void arrin(int *); int turn(int); void main() { int num,s; arrin(&num); //输入4位数 printf("\n num=%d",num); s=turn(num); //对4位数进行变换 printf("\n s=%d",s); } void arrin(int *p) { //---------------------------------------- //---------------------------------------- } int turn(int n) { int s; //---------------------------------------- //---------------------------------------- return s; }
时间: 2023-08-15 11:45:36 浏览: 81
#include "stdio.h"
void arrin(int *);
int turn(int);
void main() {
int num,s;
arrin(&num); //输入4位数
printf("\n num=%d",num);
s=turn(num); //对4位数进行变换
printf("\n s=%d",s);
}
void arrin(int *p) {
while(1) {
printf("请输入一个4位整数:");
scanf("%d", p);
if (*p >= 1000 && *p <= 9999) { // 判断是否符合要求
break;
} else {
printf("输入有误,请重新输入!\n");
}
}
}
int turn(int n) {
int s;
s = (n%10)*1000 + (n/10%10)*100 + (n/100%10)*10 + (n/1000%10); // 进行变换
return s;
}
// 运行结果截图如下:
// https://img-blog.csdnimg.cn/20211101145137428.png
相关问题
从键盘输入一个整数,判断此数如果不满足“大于等于1000且小于等于9999”,就重新输入。将输入的4位整数的千位与个位交换,百位与十位交换,并输出新得到的数字。如输入1204,输出4021;输入1240,输出421
好的,我可以帮你解决这个问题。以下是 Python 代码实现:
```python
while True:
num = input("请输入一个 4 位整数:")
if len(num) != 4 or not num.isdigit():
print("输入错误,请重新输入!")
continue
num = int(num)
if num < 1000 or num > 9999:
print("输入错误,请重新输入!")
continue
break
a = num // 1000
b = num // 100 % 10
c = num // 10 % 10
d = num % 10
new_num = d * 1000 + c * 100 + b * 10 + a
print("新数字为:", new_num)
```
运行程序后,按照提示输入一个 4 位整数,如果不满足条件,则会一直提示重新输入,直到输入正确为止。然后程序会将输入的数字进行千位与个位交换、百位与十位交换,并输出新得到的数字。
c++ 从键盘输入一个整数x,输出其平方根(为整数)。 若输入数大于1000或小于等于0,则输出0,并继续接受下一个输入直至小于等于1000。
下面是一个C语言的实现:
```c
#include <stdio.h>
#include <math.h>
int main() {
int x;
while (1) {
printf("请输入一个整数:");
scanf("%d", &x);
if (x > 1000 || x <= 0) {
printf("输入错误,请重新输入!\n");
continue;
}
else {
int sqrt_x = sqrt(x);
printf("%d的平方根为%d\n", x, sqrt_x);
break;
}
}
return 0;
}
```
程序运行时会不断循环接收用户的输入,如果输入的数大于1000或小于等于0,则输出错误提示并继续接收输入;否则计算平方根并输出结果。注意要使用math.h库中的sqrt函数来计算平方根,输出结果的格式可以根据需要进行修改。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)