编程判断输入整数的正负性和奇偶性。
**输入格式要求:"%d" 提示信息:"Input m:"
**输出格式要求:
如果是负偶数,则输出"%d is a negative even\n"
如果是负奇数,则输出"%d is a negative odd\n"
如果是正偶数,则输出"%d is a positive even\n"
如果是正奇数,则输出"%d is a positive odd\n"
如果是0,则输出"%d is zero. It is an even\n"
程序运行示例1:
Input m:6↙
6 is a positive even
程序运行示例2:
Input m:0↙
0 is zero. It is an even
#include <stdio.h>
int main()
{
int m,n;
printf("Input m:");
scanf("%d",&n);
if(n<0)
{
m=2*n;
printf("%d is a negative even\n",m);
}
else if(n<0)
{
m=2*n-1;
printf("%d is a negative odd\n",m);
}
else if (n>0)
{
m=2*n;
printf("%d is a positive even\n",m);
}
else if(n>0)
{
m=2*n-1;
printf("%d is a positive odd\n",m);
}
}
还有优解哦
#include <stdio.h>
int main()
{
int m;
printf("Input m:");
scanf("%d", &m); //1
if (m > 0) //1
{
if (m % 2 == 0) //1
{
printf("%d is a positive even\n", m);
}
else //1
{
printf("%d is a positive odd\n", m);
}
}
else if (m < 0) //1
{
if (m % 2 == 0)
{
printf("%d is a negative even\n", m); //1
}
else
{
printf("%d is a negative odd\n", m); //1
}
}
else
{
printf("%d is zero. It is an even\n", m);
}
return 0;
}
第二种比较简便,建议使用第二种风格
|