Home>Article>Backend Development> How to determine odd and even numbers in C language
How to determine odd and even numbers in C language?
Step one: First open our DEV C software, and then click "New Source Code".
Recommended: "C Language Tutorial"
Step 2: Enter the following code on our editing page:
#includeint main() { int a; printf("请输入一个整数:"); scanf("%d",&a); if(a%2==0) printf("%d为偶数!",a); else printf("%d为奇数!"); return 0; }
Step 3: Because the question requires us to input an integer first, when we define the variable, we should define it as an integer type. Note that in the input and output functions , our integer type corresponds to "%d".
Step 4: Next we have to judge the integer we input. In C language, if is a judgment statement, so we use it to judge our integers. if (a%2==0) is a judgment code recognized by our computer.
Step 5: Because we need to output the results, not just judge, so we need to combine else to judge and output the results. See the picture below for the specific code
Finally, click "Run". After the input page pops up, enter an integer and click Enter to get the result we want. The desired result.
For more programming-related content, please pay attention to theProgramming Introductioncolumn on the php Chinese website!
The above is the detailed content of How to determine odd and even numbers in C language. For more information, please follow other related articles on the PHP Chinese website!