Home>Article>Backend Development> How to determine odd and even numbers in C language

How to determine odd and even numbers in C language

藏色散人
藏色散人 Original
2020-03-03 11:13:33 87926browse

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"

How to determine odd and even numbers in C language

Step 2: Enter the following code on our editing page:

#include int main() { int a; printf("请输入一个整数:"); scanf("%d",&a); if(a%2==0) printf("%d为偶数!",a); else printf("%d为奇数!"); return 0; }

How to determine odd and even numbers in C language

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".

How to determine odd and even numbers in C language

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.

How to determine odd and even numbers in C language

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

How to determine odd and even numbers in C language

Finally, click "Run". After the input page pops up, enter an integer and click Enter to get the result we want. The desired result.

How to determine odd and even numbers in C language

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn