编写c++程序实现功能:输入两个数,第一个数为被除数,第二个数为除数,如果除数为 0,则抛出一个异常并要求重新输入两个数;如果除数不为 0,则计算并显示结果。
时间: 2023-06-01 22:04:16 浏览: 56
```
#include <stdio.h>
#include <stdlib.h>
int main() {
int dividend, divisor, result;
while (1) {
printf("Please enter two numbers: ");
scanf("%d %d", ÷nd, &divisor);
if (divisor == 0) {
printf("The divisor can not be 0, please enter again.\n");
continue;
}
result = dividend / divisor;
printf("%d / %d = %d\n", dividend, divisor, result);
break;
}
return 0;
}
```
相关问题
c++编程题: 编写程序实现功能:输入两个数,第一个数为被除数,第二个数为除数,如果除数为 0,则抛出一个异常并要求重新输入两个数;如果除数不为 0,则计算并显示结果。
```c
#include <stdio.h>
int main() {
int dividend, divisor, result;
while (1) {
printf("Enter dividend and divisor: ");
scanf("%d%d", ÷nd, &divisor);
if (divisor == 0) { // 如果除数为 0,抛出异常并重新输入
printf("Divisor can not be zero! Please enter again.\n");
continue;
}
result = dividend / divisor; // 计算结果
printf("%d / %d = %d\n", dividend, divisor, result);
break;
}
return 0;
}
```
c++编写程序:输入两个整数,求商(整数)和余数。如果除数为0,给出错误提示)
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("请输入被除数和除数:");
scanf("%d %d", ÷nd, &divisor);
if (divisor == 0) {
printf("错误:除数不能为0!\n");
} else {
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("商:%d,余数:%d\n", quotient, remainder);
}
return 0;
}
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)