输入一个正整数判断奇偶
时间: 2024-05-06 08:20:36 浏览: 75
请问您需要使用哪种编程语言实现该功能?以下是 Python 和 JavaScript 的示例代码:
Python:
```python
num = int(input("请输入一个正整数:"))
if num % 2 == 0:
print(num, "是偶数。")
else:
print(num, "是奇数。")
```
JavaScript:
```javascript
let num = parseInt(prompt("请输入一个正整数:"));
if (num % 2 === 0) {
console.log(num + " 是偶数。");
} else {
console.log(num + " 是奇数。");
}
```
相关问题
设一个正整数判断奇偶性c语言
在C语言中,判断一个正整数是否为奇数或偶数通常通过查看该数除以2后的余数来实现。如果余数为0,则该数是偶数;如果余数为1,则该数是奇数。下面是一个简单的函数示例:
```c
#include <stdio.h>
// 定义一个函数来检查正整数的奇偶性
int check_odd_even(int num) {
if (num % 2 == 0) { // 如果余数为0,返回1代表偶数
return 0;
} else { // 否则余数为1,返回1代表奇数
return 1;
}
}
int main() {
int number;
printf("请输入一个正整数:");
scanf("%d", &number);
// 判断并打印结果
if (check_odd_even(number)) {
printf("%d是奇数。\n", number);
} else {
printf("%d是偶数。\n", number);
}
return 0;
}
```
用C语言输入一个正整数,判断奇偶
```c
#include <stdio.h>
int main() {
int num;
printf("请输入一个正整数:");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d是偶数\n", num);
} else {
printf("%d是奇数\n", num);
}
return 0;
}
```
阅读全文
相关推荐
















