请帮我编一个C语言程序,可以判断奇数偶数
时间: 2023-04-11 18:01:33 浏览: 122
当然可以,以下是一个简单的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;
}
```
希望能对你有所帮助。
相关问题
c语言编写程序判断输入值是奇数偶数
在C语言中,你可以通过取模运算(%)来判断一个整数值是否为奇数或偶数。如果一个数除以2的余数为0,那么它就是偶数;如果余数为1,则它是奇数。下面是一个简单的函数示例:
```c
#include <stdio.h>
int is_even_or_odd(int num) {
if (num % 2 == 0) {
printf("%d是偶数\n", num);
return 1; // 返回1表示偶数
} else {
printf("%d是奇数\n", num);
return 0; // 返回0表示奇数
}
}
int main() {
int input;
printf("请输入一个整数: ");
scanf("%d", &input);
// 判断并打印结果
is_even_or_odd(input);
return 0;
}
```
在这个程序中,`is_even_or_odd`函数接收一个整数作为参数,然后检查其除以2的余数。根据余数的结果返回相应的结果。
c语言输入一串字母判断奇数偶数分别输出
好的,我可以帮你解决这个问题。你可以使用以下代码来实现:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, i;
printf("请输入一串字母:");
scanf("%s", str);
len = strlen(str);
printf("奇数位上的字母为:");
for(i = 0; i < len; i += 2) {
printf("%c ", str[i]);
}
printf("\n偶数位上的字母为:");
for(i = 1; i < len; i += 2) {
printf("%c ", str[i]);
}
return 0;
}
```
在这个程序中,我们首先定义了一个字符数组 `str`,然后使用 `scanf()` 函数从用户输入中读取一串字母,并计算出字符串的长度 `len`。接着,我们使用两个循环分别输出字符串中奇数位上的字母和偶数位上的字母。在循环中,我们使用了递增的步长来跳过奇数位或偶数位上的字符。
希望这个程序能够帮到你!
阅读全文
相关推荐














